need assistance with POST url for DHL label generation api

This forum is for eXpress++ general support.
Message
Author
gnewcomb
Posts: 17
Joined: Thu Jan 28, 2010 7:06 pm
Location: Colorado Springs, Colorado USA

need assistance with POST url for DHL label generation api

#1 Post by gnewcomb »

I am getting an INVALID TOKEN response running the POST request in the below code. What am I doing wrong?

Code: Select all

PROCEDURE Main
LOCAL oHC, cAccessToken, cJS, cResult

cAccessToken := "XQlO0HszO4NB/pyHrdyeLGPeQO59X7iWVT1FOWt6Ok6foyXSomBKOpDLt5j2RcoNC6ssXYCok4y55kGEz2TAh3Vc6YqPedExx0ipn9w7Gqw5JPvLYT3kv8CEf2sbHt7W+g/SlhtL5ajbF/WF2ucXl8TYAqz1fa9zf22XCCG8s/MM2qlkiyq/yM1stXSrdKjyq+raNvdwLifynpQ3EaeXA4IDbV1kjaGiUwgSSLLIKto="

oHC := HttpClient():new( "https://api.dhlglobalmail.com/v2/label/multi/image" )

oHC:setAcceptType( "application/json" )
oHC:setAcceptCharSet( "utf-8" )

oHC:httpRequest:setContentType( "application/json" )
oHC:httpRequest:setContentCharSet( "utf-8" )

oHC:httpRequest:setParameter( "access_token", cAccessToken )
oHC:httpRequest:setParameter( "client_id", "28363"  )

oHC:httpRequest:setMethod( "POST" )

cJS := "valid json goes here"

oHC:httpRequest:setContent( cJs )

cResult := oHC:send()

wtf oHC:httpRequest:getUri(), oHC:httpRequest:getQueryString(), cResult pause

RETURN

Code: Select all

cResult := {"meta":{"error":[{"error_message":"Access Token provided was invalid","error_type":"INVALID_TOKEN"}],"timestamp":"2018-03-12T15:26:15-04:00","code":400}}
I can execute a POST https://api.dhlglobalmail.com/v2/label/ ... t_id=28363 and get the correct expected response.

Code: Select all

{
    "meta": {
        "error": [
            {
                "error_message": "Request did not contain valid JSON",
                "error_type": "INVALID_JSON"
            }
        ],
        "timestamp": "2018-03-12T15:07:41-04:00",
        "code": 400
    }
}

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: need assistance with POST url for DHL label generation a

#2 Post by rdonnay »

What does WTF oHC:httpRequest:getQueryString() return?

Also, have you tried using LoadFromUrl() ?
The eXpress train is coming - and it has more cars.

gnewcomb
Posts: 17
Joined: Thu Jan 28, 2010 7:06 pm
Location: Colorado Springs, Colorado USA

Re: need assistance with POST url for DHL label generation a

#3 Post by gnewcomb »

Code: Select all

oHC:httpRequest:getQueryString()=access_token=XQlO0HszO4NB/pyHrdyeLGPeQO59X7iWVT1FOWt6Ok6foyXSomBKOpDLt5j2RcoNC6ssXYCok4y55kGEz2TAh3Vc6YqPedExx0ipn9w7Gqw5JPvLYT3kv8CEf2sbHt7W+g/SlhtL5ajbF/WF2ucXl8TYAqz1fa9zf22XCCG8s/MM2qlkiyq/yM1stXSrdKjyq+raNvdwLifynpQ3EaeXA4IDbV1kjaGiUwgSSLLIKto=&client_id=28363
I haven't tried LoadFromURL().

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: need assistance with POST url for DHL label generation a

#4 Post by rdonnay »

The eXpress train is coming - and it has more cars.

gnewcomb
Posts: 17
Joined: Thu Jan 28, 2010 7:06 pm
Location: Colorado Springs, Colorado USA

Re: need assistance with POST url for DHL label generation a

#5 Post by gnewcomb »

Same result using "https://api.dhlglobalmail.com/v2/label/multi/image.json"

I assume alaska adds on the .json based on the content type. I tried LoadFromUrl also and get the same result.

Could it have anything to do with the charset being used?
I tried

Code: Select all

oHC:httpRequest:setParameter( "access_token", Char2UTF8(cAccessToken) )
but got the same result doing that also.

Here are docs for the api https://api.dhlglobalmail.com/docs/v2/label.html The access token I generated is good for 24 hours.

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: need assistance with POST url for DHL label generation a

#6 Post by rdonnay »

Could it have anything to do with the charset being used?
That was my first thought.

When I have problems like this, I try to debug them using Fiddler.
This displays your http traffic so you can see exactly what's being sent.
The eXpress train is coming - and it has more cars.

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: need assistance with POST url for DHL label generation a

#7 Post by rdonnay »

I get the same result when running from Xbase++, however when I run from Chrome I get a 404 - File not found.
The eXpress train is coming - and it has more cars.

gnewcomb
Posts: 17
Joined: Thu Jan 28, 2010 7:06 pm
Location: Colorado Springs, Colorado USA

Re: need assistance with POST url for DHL label generation a

#8 Post by gnewcomb »

I get the same result when running from Xbase++, however when I run from Chrome I get a 404 - File not found.
You need to do a POST instead of GET on the url. I am using the Postman app and it works correctly.
Attachments
3-12-2018 5-55-15 PM.png
3-12-2018 5-55-15 PM.png (44.61 KiB) Viewed 14596 times

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: need assistance with POST url for DHL label generation a

#9 Post by rdonnay »

I used an html form to generate a post and I get the same token error.

Something else is wrong. Maybe the token really has expired.

Code: Select all

<form METHOD="POST" ACTION="https://api.dhlglobalmail.com/v2/label/multi/image.json">

<input type="hidden" name="access_token" value="XQlO0HszO4NB/pyHrdyeLGPeQO59X7iWVT1FOWt6Ok6foyXSomBKOpDLt5j2RcoNC6ssXYCok4y55kGEz2TAh3Vc6YqPedExx0ipn9w7Gqw5JPvLYT3kv8CEf2sbHt7W+g/SlhtL5ajbF/WF2ucXl8TYAqz1fa9zf22XCCG8s/MM2qlkiyq/yM1stXSrdKjyq+raNvdwLifynpQ3EaeXA4IDbV1kjaGiUwgSSLLIKto=">

<input type="hidden" name="client_id" value="28363">

<input type="submit" name="submit" value="submit">

</form>
The eXpress train is coming - and it has more cars.

gnewcomb
Posts: 17
Joined: Thu Jan 28, 2010 7:06 pm
Location: Colorado Springs, Colorado USA

Re: need assistance with POST url for DHL label generation a

#10 Post by gnewcomb »

I don't think it is a token expiration issue. Below is the json for a new token I just generated.

Code: Select all

{"meta":{"timestamp":"2018-03-13T10:16:30-04:00","code":200},"data":{"access_token":"9WtrPTkTRF3p1Ehc41PUvPymzvp2VeyLXeToljiA7qGfoyXSomBKOpDLt5j2RcoNC6ssXYCok4y55kGEz2TAh3Vc6YqPedExx0ipn9w7Gqw5JPvLYT3kv8CEf2sbHt7W+g/SlhtL5ajbF/WF2ucXl8TYAqz1fa9zf22XCCG8s/MM2qlkiyq/yM1stXSrdKjyq+raNvdwLifynpQ3EaeXA4IDbV1kjaGiUwgSSLLIKto=","expires_in":86400,"scope":"prealerts,return_label,status,encode,closeout,sortcode,label,manifest_session,accounts,locations,events,products,mailitems"}}

Post Reply