POST Request getting 401 when using a variable

Hi everyone!

Apologies in advance if this is silly but I hope to find some resolution to this issue :slight_smile:

I am sending a POST request that uses a collection variable - I can confirm from Console that the request body is resolving the variable correctly. However, the response is still 401.

As soon as I replace the variable with the hard-coded value, seems to work fine. So… what’s up?

I appreciate any help! Please let me know if I can provide more information.

Thank you! :slight_smile:

1 Like

Hi, @tdas05 Welcome to the community :slight_smile:

Queries we raise is never silly so don’t be sorry for it :slight_smile:

To understand your issue more clearly could you please share some snippet/screenshots and more details on the issue you are facing.

4 Likes

Thank you for the reply, Stella!

I am basically sending raw JSON parameters like so:

    "Organisations": [
            {
                "OrganisationId": "{{Org1Id}}"
            }

Where Org1Id is my collection variable. I have it set to persist values so I can confirm that initial and current values are set. I can also confirm that the value for variable is being resolved correctly (as seen in Console) on request:

image

However, on running the POST request, I see a 401.

If I manually replace the variable with a GUID without changing anything else in the request, it works just fine - 200 OK.

Cheers!

1 Like

Hi, @tdas05 I’m able to process the Collection variable without any issue

It seems to be a simple mistake that you are making if possible connect with me on jency.stella19@gmail.com via google chat I’m availabe online now happy to help you :slight_smile:

4 Likes

Can you also add your pre and post scripts

1 Like

Thank you so much for the responses, everyone!

Here’s my post-request script (I do not have a pre-request script). It basically saves the value from identifier of the entity to a collection variable:

pm.test("Status code is 200", () => 
{
  pm.response.to.have.status(200);
});
var data = JSON.parse(responseBody);
pm.collectionVariables.set("App1Id", data.EntityId);

I will connect with you on gtalk, @jency.stella19! Thank you!

2 Likes

Hi everyone!

Just a quick update that Jency and I had a catch-up and it did not seem like anything should obviously have prevented the request from going through.

As of now, the issue remains unresolved :confused:

Please let me know if you’d like more information.

Thank you!

Hi,

Are you perhaps testing against a mock server that you had set up against your collection? If you are, then I have a couple of ideas what the issue may be.

Hey there!

I am sending requests to a dedicated Web Server :confused:
I am more than happy to give your ideas a go, anyhow?

Cheers,
Tisha.

Unfortunately my ideas relate specifically to calls to a Postman mock server.

It’s a bit difficult to explain and most probably quite confusing.
I’ve run into issues before where it seemed that the variable was not populated but it was simply because the mock example body contained the same variable as the request body. The problem was that the variable was defined in a specific environment (Env-A). The collection was set to use Env-A but the mock server was set to use Env-B. Unfortunately the variable was in Env-B too but with a different value. Therefor the mock server could not find the appropriate response when comparing the request body values since the values differed when calling from the collection vs on the mock server side.

Unfortunately I don’t think this applies to your situation though.

2 Likes

Because you are getting a 401 Unauthorized response, can I assume that the OrganisationId is used in your authentication process? In other words, authentication fails when you send in {{Org1Id}} but succeeds when sending in the actual “hardcoded” value?

I suggest you make use of the Postman Echo API ( Echo API | Postman Learning Center) It will allow you to see exactly what you are sending in with your request and what the value is that your request body contains. While I cannot explain your result it is clear to me that what you think your variable is sending in and what is actually sent, is most probably not that same.

Good luck.

1 Like

Hi @tdas05 ,

Going through this thread, it seems variable is populating properly.

Only thing which I can think of is the data set in collection variable might be different than actual value.

Can you please compare the guid values in notepad. Even if there is case difference of character it will be a different guid.

It is just a thought.

1 Like

Thank you for the suggestion, @gerhard-finclude!

I tried to send a variable in my request to the ECHO endpoint and worked perfectly there:

This leads to me suspect that it is an infrastructure issue on the server! I will investigate this further!

Cheers,
Tisha :slight_smile:

Hey @darkphoenix, unfortunately it seems like the values are identical :confused:

As I noted in my previous comment, I suspect it’s an issue with the end point of the server I am making the calls on.

I will keep everyone posted! :slight_smile:

1 Like

At least you’ve ruled out the issue on your side and can now prove what values you are sending through. :slight_smile:

Good luck.

1 Like

Hello everyone!

Big news! It is now fixed - the issue was with our authorization pre request script.

I was previously using:

var requestBodyBase64 = "";
if (request.method != 'GET' && request.data) {
var md5 = CryptoJS.MD5(request.data.toString());  
requestBodyBase64 = CryptoJS.enc.Base64.stringify(md5);
}

The request.data was including the {{Variable}} and postman replaces it before sending.

I tried:

var md5 = CryptoJS.MD5(
pm.variables.replaceIn(pm.environment.replaceIn(request.data.toString()
));

And it now works! :slight_smile:

Please let me know if you need more information but I hope this helps anyone encountering a similar issue!

Cheers,
Tisha.

4 Likes

Great to see the answer posted :wave::clap:

1 Like