Results 1 to 7 of 7
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277

    WinHTTP Request 5.1 error 400 (Bad Request)

    Hello,



    I am trying to integrate creating shipping labels in my Access application. I have good documentation from the carrier and I made the auth request work using the Postman software, as can be seen here:

    https://i.imgur.com/9X93SgH.png

    https://i.imgur.com/cTcPyWU.png

    https://i.imgur.com/bKyv2b1.png

    I am trying to replicate this using VBA in Access, with this code:

    Code:
    Public Sub PPLAuth()
    
        Dim HTTPreq As Object
        Dim URL As String
        Dim strBody As String
        Dim JSON As Object
    
        Set HTTPreq = CreateObject("WinHttp.WinHttpRequest.5.1")
        
        URL = "https://api-dev.dhl.com/ecs/ppl/myapi2/login/getAccessToken"
        
        HTTPreq.Open "POST", URL, False
        HTTPreq.SetRequestHeader "content-type", "application/x-www-form-urlencoded"
        HTTPreq.SetRequestHeader "user-agent", "PostmanRuntime/7.32.2"
        HTTPreq.SetRequestHeader "accept", "*/*"
        HTTPreq.SetRequestHeader "cache-control", "no-cache"
        HTTPreq.SetRequestHeader "postman-token", "52edacc2-5d59-41d4-beec-5f5448e5f92e"
        HTTPreq.SetRequestHeader "host", "api-dev.dhl.com"
        HTTPreq.SetRequestHeader "accept-encoding", "gzip, deflate, br"
        HTTPreq.SetRequestHeader "connection", "keep-alive"
        HTTPreq.SetRequestHeader "content-length", "109"
        
        strBody = "{""grant_type"": ""client_credentials"", ""scope"": ""myapi2"", ""client_id"": ""zzzzzz"", ""client_secret"": ""xxxxxxx""}"
        
        Debug.Print strBody
        HTTPreq.send strBody
        
        Set JSON = JsonConverter.ParseJson(HTTPreq.responseText)
        Debug.Print HTTPreq.responseText
        
        Set HTTPreq = Nothing
        Set JSON = Nothing
    
    End Sub
    But unfortunately, here I don't get a 200 OK response. I get this:

    Code:
    {"status":400,"title":"Bad Request","detail":"One or more required form parameters are missing."}
    Since it works in the Postman app, it must be something VBA related that I am not formatting correctly. Can you please help me which part is incorrect in VBA?

    Thanks!
    Tomas

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    What does the output of Debug.Print strBody show?
    Why the double double quotes?, when the header detail has only single double quotes?

    Perhaps have a look here and here
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277
    Quote Originally Posted by Welshgasman View Post
    What does the output of Debug.Print strBody show?
    Why the double double quotes?, when the header detail has only single double quotes?

    Perhaps have a look here
    Debug.Print strBody shows this:
    Code:
    {"grant_type": "client_credentials", "scope": "myapi2", "client_id": "yyyyy", "client_secret": "zzzzz"}
    Double quotes because that gives single quotes instead of string end in VBA. I am assuming at this point that body needs to be included with the Send method as a single string, whereas header requests must be included separately - hence the difference between header/body.

    You sent a link to Postman testing. That is exactly what I am doing though, as can be seen in the imgur pictures in the initial post :-)

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    See the second link and the first link from that.

    Is content-length actually meant to be a string?

    I do notice that link uses a different HttpReq object?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277
    Which link do you mean? Are you aware that every person can potentially get different search results on Google?

    I tried to remove the quotes from content-length parameter, that did not change anything.

    What do you mean by "link uses different object"?

  6. #6
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    309
    Your postman request shows there are 8 hidden headers.
    The message you got is informing you that the body is wrong.
    It's quite likely that a lot of those headers aren't even necessary.
    You probably only need content-type.
    And your content type is a JSON string, not url encoded.

    Try to send this as the body instead, modify with your credentials.
    grant_type=client_credentials&scope=myapi2&client_ id=zzzzzz&client_secret=xxxxxxx
    Please click on the ⭐ below if this post helped you.


  7. #7
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    277
    Quote Originally Posted by Edgar View Post
    Your postman request shows there are 8 hidden headers.
    The message you got is informing you that the body is wrong.
    It's quite likely that a lot of those headers aren't even necessary.
    You probably only need content-type.
    And your content type is a JSON string, not url encoded.

    Try to send this as the body instead, modify with your credentials.
    grant_type=client_credentials&scope=myapi2&client_ id=zzzzzz&client_secret=xxxxxxx
    Yes, I just found the same thing as you wrote this. The problem was that the integration uses x-www-form-urlencoded, which requires your proposed syntax. Now I successfully got the token, thank you!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 45
    Last Post: 05-26-2024, 10:00 AM
  2. Database Bloat on DAO CommitTrans by 400 kb/commit
    By angeleumbra in forum Programming
    Replies: 7
    Last Post: 06-09-2022, 11:17 AM
  3. winhttp response text
    By hemaazez in forum Access
    Replies: 3
    Last Post: 05-23-2017, 06:01 AM
  4. Replies: 1
    Last Post: 05-02-2014, 09:29 AM
  5. Importing Time Data from AS/400
    By smunie in forum Import/Export Data
    Replies: 0
    Last Post: 11-17-2006, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums