Jump to content

Recommended Posts

Posted (edited)

the "Dropbox-Api-Result" field (7th line in the output below) contains a result formatted in JSON.  trying to separate it out to read values from it

$raw_output = 'HTTP/1.1 200 OK' & @LF
$raw_output &= 'Content-Type: application/octet-stream' & @LF
$raw_output &= 'Accept-Ranges: bytes' & @LF
$raw_output &= 'Cache-Control: no-cache' & @LF
$raw_output &= 'Content-Disposition: attachment; filename=unspecified' & @LF
$raw_output &= 'Content-Security-Policy: sandbox' & @LF
$raw_output &= 'Dropbox-Api-Result: {"name": "2025-02-03 08.03.49.jpg", "path_lower": "/camera uploads/2025-02-03 08.03.49.jpg", "path_display": "/Camera Uploads/2025-02-03 08.03.49.jpg", "id": "id:T45AAw", "client_modified": "2025-02-03T16:03:49Z", "server_modified": "2025-02-03T16:50:32Z", "rev": "62d3fb0cc7a260a", "size": 2296979, "media_info": {".tag": "metadata", "metadata": {".tag": "photo", "dimensions": {"height": 2268, "width": 4032}, "time_taken": "2025-02-03T16:03:49Z"}}, "is_downloadable": true, "content_hash": "2c2b30a6447b5a37301e533fg82269db1b06"}' & @LF
$raw_output &= 'Etag: W/"62d3fb0dc20c7a26"' & @LF
$raw_output &= 'Original-Content-Length: 22969' & @LF
$raw_output &= 'Vary: Dropbox-API-Arg, Authorization' & @LF
$raw_output &= 'X-Content-Security-Policy: sandbox' & @LF
$raw_output &= 'X-Content-Type-Options: nosniff' & @LF
$raw_output &= 'X-Server-Response-Time: 1357' & @LF
$raw_output &= 'X-Webkit-Csp: sandbox' & @LF
$raw_output &= 'Date: Tue, 04 Feb 2025 19:02:02 GMT' & @LF
$raw_output &= 'Server: envoy' & @LF
$raw_output &= 'Strict-Transport-Security: max-age=31536000; includeSubDomains; preload' & @LF
$raw_output &= 'X-Robots-Tag: noindex, nofollow, noimageindex' & @LF
$raw_output &= 'Content-Length: 2296979' & @LF
$raw_output &= 'X-Dropbox-Response-Origin: far_remote' & @LF
$raw_output &= 'X-Dropbox-Request-Id: 839cba708b480815072cca784' & @LF

$json = StringRegExpReplace($raw_output, "Dropbox-Api-Result: {([^,]*)}", "")

thanks in advance!

Edited by gcue
  • Solution
Posted (edited)
#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <jq\jq.au3>


example()

Func example()

    Local $raw_output
    Local $sJson = "", $sResult = ""
    Local $aResult

    ;Raw data
    $raw_output = 'HTTP/1.1 200 OK' & @LF
    $raw_output &= 'Content-Type: application/octet-stream' & @LF
    $raw_output &= 'Accept-Ranges: bytes' & @LF
    $raw_output &= 'Cache-Control: no-cache' & @LF
    $raw_output &= 'Content-Disposition: attachment; filename=unspecified' & @LF
    $raw_output &= 'Content-Security-Policy: sandbox' & @LF
    $raw_output &= 'Dropbox-Api-Result: {"name": "2025-02-03 08.03.49.jpg", "path_lower": "/camera uploads/2025-02-03 08.03.49.jpg", "path_display": "/Camera Uploads/2025-02-03 08.03.49.jpg", "id": "id:T45AAw", "client_modified": "2025-02-03T16:03:49Z", "server_modified": "2025-02-03T16:50:32Z", "rev": "62d3fb0cc7a260a", "size": 2296979, "media_info": {".tag": "metadata", "metadata": {".tag": "photo", "dimensions": {"height": 2268, "width": 4032}, "time_taken": "2025-02-03T16:03:49Z"}}, "is_downloadable": true, "content_hash": "2c2b30a6447b5a37301e533fg82269db1b06"}' & @LF
    $raw_output &= 'Etag: W/"62d3fb0dc20c7a26"' & @LF
    $raw_output &= 'Original-Content-Length: 22969' & @LF
    $raw_output &= 'Vary: Dropbox-API-Arg, Authorization' & @LF
    $raw_output &= 'X-Content-Security-Policy: sandbox' & @LF
    $raw_output &= 'X-Content-Type-Options: nosniff' & @LF
    $raw_output &= 'X-Server-Response-Time: 1357' & @LF
    $raw_output &= 'X-Webkit-Csp: sandbox' & @LF
    $raw_output &= 'Date: Tue, 04 Feb 2025 19:02:02 GMT' & @LF
    $raw_output &= 'Server: envoy' & @LF
    $raw_output &= 'Strict-Transport-Security: max-age=31536000; includeSubDomains; preload' & @LF
    $raw_output &= 'X-Robots-Tag: noindex, nofollow, noimageindex' & @LF
    $raw_output &= 'Content-Length: 2296979' & @LF
    $raw_output &= 'X-Dropbox-Response-Origin: far_remote' & @LF
    $raw_output &= 'X-Dropbox-Request-Id: 839cba708b480815072cca784' & @LF

    ;Parse JSON from raw output
    $aResult = StringRegExp($raw_output, "(?m)^Dropbox-Api-Result: (.*)", $STR_REGEXPARRAYMATCH)
    If @error Then
        Switch @error
            Case 1 ;No match found
                Return MsgBox($MB_ICONWARNING, "Warning", "No match found.")
            Case 2 ;Bad regex pattern
                Exit MsgBox($MB_ICONERROR, "Error", "Bad regex pattern. @extended = " & @extended)
        EndSwitch
    EndIf
    $sJson = $aResult[0]

    ;Pretty print JSON
    _jqInit("C:\Utils\JQ\jq-win64.exe") ; Only needed if jq exe is not in @scriptDir or PATH
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to find jq executable - @error = " & @error)
    ConsoleWrite("Formatted JSON:"          & @CRLF)
    ConsoleWrite(_jqPrettyPrintJson($sJson) & @CRLF)

    ;Get and display some JSON info using jq string interpolation
    $sResult = _jqExec($sJson, '"\"\(.name)\" is a \(.media_info.metadata.".tag")."' _
                       )
    ConsoleWrite(@CRLF)
    ConsoleWrite("Result:" & @CRLF)
    ConsoleWrite($sResult  & @CRLF)

EndFunc

Result

Formatted JSON:
{
  "name": "2025-02-03 08.03.49.jpg",
  "path_lower": "/camera uploads/2025-02-03 08.03.49.jpg",
  "path_display": "/Camera Uploads/2025-02-03 08.03.49.jpg",
  "id": "id:T45AAw",
  "client_modified": "2025-02-03T16:03:49Z",
  "server_modified": "2025-02-03T16:50:32Z",
  "rev": "62d3fb0cc7a260a",
  "size": 2296979,
  "media_info": {
    ".tag": "metadata",
    "metadata": {
      ".tag": "photo",
      "dimensions": {
        "height": 2268,
        "width": 4032
      },
      "time_taken": "2025-02-03T16:03:49Z"
    }
  },
  "is_downloadable": true,
  "content_hash": "2c2b30a6447b5a37301e533fg82269db1b06"
}

Result:
"2025-02-03 08.03.49.jpg" is a photo.

 

Edited by TheXman
Posted

this output is similar to the one above but it has a string before the JSON and 2 line breaks.  i tried to use the regex above to account for the string.. but not sure how to account for 2 line breaks?

$raw_output = 'HTTP/1.1 200 OK' & @LF
$raw_output &= 'Content-Type: application/json' & @LF
$raw_output &= 'Cache-Control: no-cache' & @LF
$raw_output &= 'X-Content-Type-Options: nosniff' & @LF
$raw_output &= 'X-Server-Response-Time: 1010' & @LF
$raw_output &= 'Date: Wed, 05 Feb 2025 15:27:50 GMT' & @LF
$raw_output &= 'Server: envoy' & @LF
$raw_output &= 'Content-Length: 431' & @LF
$raw_output &= 'Strict-Transport-Security: max-age=31536000; includeSubDomains; preload' & @LF
$raw_output &= 'X-Robots-Tag: noindex, nofollow, noimageindex' & @LF
$raw_output &= 'Vary: Accept-Encoding' & @LF
$raw_output &= 'X-Dropbox-Response-Origin: far_remote' & @LF
$raw_output &= 'X-Dropbox-Request-Id: c34ba46094f64fcda2752ad7c9a02b0f' & @LF
$raw_output &= '' & @LF
$raw_output &= '{"name": "PTT-20240620-WA0002.opus", "path_lower": "/camera uploads/ptt-20240620-wa0002.opus", "path_display": "/Camera Uploads/PTT-20240620-WA0002.opus", "id": "id:Bl5VOgKPuG8AAAAAAAH6MA", "client_modified": "2025-02-05T15:27:50Z", "server_modified": "2025-02-05T15:27:50Z", "rev": "62d66c4b79f0f0c7a269a", "size": 26860, "is_downloadable": true, "content_hash": "08a0d743e12e34868685c8864dd500ae21c74c27ae49f1d43cb64d843e08f711"}' & @LF

$aResult = StringRegExp($raw_output, "(?m)^X-Dropbox-Request-Id: [0-9A-Za-z]+(.*)", $STR_REGEXPARRAYMATCH)
If @error Then
    Switch @error
        Case 1 ;No match found
;~          Return
            MsgBox($MB_ICONWARNING, "Warning", "No match found.")
        Case 2 ;Bad regex pattern
            Exit MsgBox($MB_ICONERROR, "Error", "Bad regex pattern. @extended = " & @extended)
    EndSwitch
EndIf
_ArrayDisplay($aResult)
$sJson = $aResult[0]

 

Posted

Instead of parsing the values of http response headers manually, why don't you use something that will retrieve individual header values for you like: winhttp api, winhttp com, or curl?  What are you currently using to get the http response?

Posted (edited)

using curl - this is the response i am getting from the curl request. 

i am uploading a file and the content type i am specifying is Content-Type: application/octet-stream
i am adding  -D - to the curl request to get a response. 

Edited by gcue
Posted (edited)

If you are using curl and you are looking specifically for the value of the "Dropbox-Api-Result" response header, then I would add the  -w / --write-output parameter to the command line to get that value.  To get just that value, it would look something like -w %header{Dropbox-Api-Result}.  If you want all of the headers in an easy to process JSON format, then I would use -w %{header_json} and use your favorite json udf to get the values of interest.

 

The example below will return just the value of the date response header:

curl -s -w %header{date} -o nul https://www.autoitscript.com

curl -s -w "%header{date}" -o nul https://www.autoitscript.com

 

Edited by TheXman
Posted (edited)

love your idea! not getting back the header.

curl request:

$CURL_EXE = "c:\windows\system32\curl.exe"
$METHOD = "-X POST"
$API_URL = "https://content.dropboxapi.com/2/files/download"
$HEADERS_1 = "Authorization: Bearer " & $token
$HEADERS_2 = 'Dropbox-API-Arg: {\"path\":\"' & $iSource_Path & '\"}'
$HEADERS_3 = $iTarget_Path
 $iPID = Run($sCmdLine, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

here are the different variations i've tried - cant seem to get just the header

$sCmdLine = StringFormat('%s %s --header "%s" --header "%s" --output "%s" -D --write-output %header{Dropbox-Api-Result}', $CURL_EXE, $API_URL, $HEADERS_1, $HEADERS_2, $HEADERS_3)

$sCmdLine = StringFormat('%s %s --header "%s" --header "%s" --output "%s" -D - --write-output %header{Dropbox-Api-Result}', $CURL_EXE, $API_URL, $HEADERS_1, $HEADERS_2, $HEADERS_3)

$sCmdLine = StringFormat('%s %s --header "%s" --header "%s" --output "%s" -D - -w %header{Dropbox-Api-Result}', $CURL_EXE, $API_URL, $HEADERS_1, $HEADERS_2, $HEADERS_3)

$sCmdLine = StringFormat('%s %s --header "%s" --header "%s" --output "%s" -w %header{Dropbox-Api-Result}', $CURL_EXE, $API_URL, $HEADERS_1, $HEADERS_2, $HEADERS_3)

$sCmdLine = StringFormat('%s %s --header "%s" --header "%s" --output "%s" --write-output %header{Dropbox-Api-Result}', $CURL_EXE, $API_URL, $HEADERS_1, $HEADERS_2, $HEADERS_3)

thank you soooo much for your help!!!

Edited by gcue
Posted (edited)

The "%" in StringForma()t has to be escaped when you actually want a literal "%" character.  You do it by using %% (like %%header{Dropbox-Api-Result}).  If you would have used ConsoleWrite to view your command line, you would have seen that it did not produce your desired result.

Edited by TheXman
Posted

In case it's not understandable what @TheXman mentioned already, you can use "%{header_json}" to see which headers can be useful for you.

curl -s -w "%{header_json}" -o nul https://content.dropboxapi.com/2/files/download

Then pick the specific one and hopefully this will help you 😀 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...