Page 1 of 2

need assistance with POST url for DHL label generation api

Posted: Mon Mar 12, 2018 12:33 pm
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
    }
}

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

Posted: Mon Mar 12, 2018 12:57 pm
by rdonnay
What does WTF oHC:httpRequest:getQueryString() return?

Also, have you tried using LoadFromUrl() ?

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

Posted: Mon Mar 12, 2018 1:14 pm
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().

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

Posted: Mon Mar 12, 2018 1:56 pm
by rdonnay

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

Posted: Mon Mar 12, 2018 2:17 pm
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.

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

Posted: Mon Mar 12, 2018 2:32 pm
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.

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

Posted: Mon Mar 12, 2018 2:46 pm
by rdonnay
I get the same result when running from Xbase++, however when I run from Chrome I get a 404 - File not found.

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

Posted: Mon Mar 12, 2018 3:01 pm
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.

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

Posted: Mon Mar 12, 2018 3:28 pm
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>

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

Posted: Tue Mar 13, 2018 7:32 am
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"}}