Getting a header from post response?

Hey everyone, Has anyone been successful at extracting a header from a post response?
i looked everywhere and found lots of setting post headers but not extracting a new one thats included in the response. for example if the new header was named operation-location and the value was http://somesite.com/123456
thanks

1 Like

I’m not sure what you mean. Can you post an example of the JSON you’re trying to parse?

1 Like

there is no body, microsoft puts everything in the header for some odd reason. here is the header response

HTTP/1.1 202 Accepted
Content-Length: 0
Operation-Location: https://somesite.cognitiveservices.azure.com/formrecognizer/documentModels/prebuilt-layout/analyzeResults/3748495-sdjmnsd-dsjndsj4u?api-version=2023-02-28-preview
x-envoy-upstream-service-time: 310
apim-request-id: f3748495-sdjmnsd-dsjndsj4u
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-ms-region: East US
Date: Sun, 04 Jun 2023 13:07:52 GMT
Connection: close
1 Like

What blocks are you using in Thunkable? Are you getting a JSON response at all?

1 Like

Hello yes I get the 202 status and depending on how I configure the blocks for response I get either null or undefined . Which I believe is because response only contains body .
Here’s the blocks I’m using except I use a post instead of get and I have tried configuring them multiple ways

1 Like

But you’re not getting a JSON response which is the main problem. Posting a screenshot of a different block isn’t going to help to troubleshoot this issue.

The Post block for OpenWeather’s API needs to be configured a certain way. If you’re doing all of that configuration in the properties panel for the API, I won’t be able to help you without having a link to your project. Because I can’t see the Body, Headers, etc. that you’re using.

Are you using the OpenWeatherMap API? Which endpoint of the API are you accessing? What is the exact URL?

i found a solution using the web viewer.

<!DOCTYPE html>
<html>
<head>
  <title>Fetch Request</title>
  <script src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js" type="text/javascript"></script>
</head>
<body>
  <script type="text/javascript">
    // Your JavaScript code here...
    fetch("URL", {
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
            "Ocp-Apim-Subscription-Key": "my key"
        },
        "body": JSON.stringify({
            "urlSource": "my image location"
        })
    })
    .then(response => {
        const operationLocation = response.headers.get('operation-location');
        // Send the operationLocation to Thunkable:
        ThunkableWebviewerExtension.postMessage(operationLocation);
    })
    .catch(err => {
        // Handle errors here
        console.error(err);
    });
  </script>
</body>
</html>

Hello @mcdonaldzacharyaj3o6, Yes, it is certainly possible to extract a header from a POST response. The specific method will depend on the language and library you are using. For instance, in Python using the requests library, you could do it this way:

python code

import requests

response = requests.post('http://yourwebsite.com', data={'key':'value'})
header_value = response.headers['operation-location']

In the above code, replace 'http://yourwebsite.com' with the actual URL you’re making the POST request to, and replace 'key':'value' with your actual data. 'operation-location' is the name of the header you want to extract.

If the header does not exist, it will raise a KeyError. To avoid this, you can use the get() method which returns None if the header does not exist:

python code

header_value = response.headers.get('operation-location')

If you’re using a different language or library, the exact code will be different, but the general concept will be the same. If you could provide more details about your specific situation, I could possibly provide a more tailored answer.

Not to take away from what you’re saying @piperthomas4765h2yxh but this seems like a lot of trouble for something that I’m pretty sure just requires the Web API blocks in Thunkable. If they are using OpenWeatherMap’s API, it’s really straightforward to get any data they want from the JSON response.

Hi, I have run into the exact same issue as @mcdonaldzacharyaj3o6

I called web_api’s post block and want to return the Operation-Location header in the response
response.headers[“Operation-Location”]

Here’s my current block screenshot, I’m returning status 202 and null response. My understanding is that thunkable Web API block allows you to access the body of the response (i.e., the actual data returned by the server), but not the headers (which is what I need)
Thanks