We help IT Professionals succeed at work.

curl to HTTP request VBA

Gontran Harvey
on
2,559 Views
Last Modified: 2017-03-15
Hi, I have a curl that I want to send to API. (ACT! CRM).
The GET request work's with the code below:

Here the curl:
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer xxxxxxxxxxxx' 'http://pc-gontran/Act.Web.API/api/Contacts'
Here the code:
'*********************************************************************************
    oXMLHTTP.Open "GET", "http://pc-gontran/Act.Web.API/api/Contacts", False
   oXMLHTTP.setRequestHeader "Content-Type: ", "application/json" ''Content-Type: application/json'
   oXMLHTTP.setRequestHeader "Accept: ", "application/json"
   oXMLHTTP.setRequestHeader "Authorization: ", "Bearer " & strBearer
   
    oXMLHTTP.Send
Do Until oXMLHTTP.ReadyState = 4
        DoEvents
    Loop
   
    Me.txt_Status = oXMLHTTP.Status
    If oXMLHTTP.Status = 200 Then
        strHTML = oXMLHTTP.ResponseText
        Me.txt_Response = strHTML
    End If
'*********************************************************************************

Now when I try the same whit this curl:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Bearer xxxxxxxxxxxxxxx' -d '{}' 'http://pc-gontran/Act.Web.API/api/Contacts'

Whit this code:
'*********************************************************************************
    oXMLHTTP.Open "POST", "http://pc-gontran/Act.Web.API/api/Contacts", False
   oXMLHTTP.setRequestHeader "Content-Type: ", "application/json" ''Content-Type: application/json'
   oXMLHTTP.setRequestHeader "Accept: ", "application/json"
   oXMLHTTP.setRequestHeader "Authorization: ", "Bearer " & strBearer
   
    oXMLHTTP.Send ("'{}'")
Do Until oXMLHTTP.ReadyState = 4
        DoEvents
    Loop
   
    Me.txt_Status = oXMLHTTP.Status
    If oXMLHTTP.Status = 200 Then
        strHTML = oXMLHTTP.ResponseText
        Me.txt_Response = strHTML
    End If
'*********************************************************************************
When I try the curl in the API UI, it's working, but not in VBA (i'm in Access buy the way), I received a 400 error (bad server request).

Do you know what i,m doing wong?
Comment
Watch Question

Senior Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Already try that and got the same issue.
tks
ste5anSenior Developer
CERTIFIED EXPERT

Commented:
And Fiddler?

Author

Commented:
I used it and I found that oXMLHTTP.Status = 201 instead of 200 when it's working!!
Whit Fidder I was able to see that it was working.
And you where right about the simple quote.

Thanks for everything

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.