how to consume API using Golang

136 views
Skip to first unread message

Asim Shahzad

unread,
Jun 17, 2021, 12:19:45 PM6/17/21
to golang-nuts

Please anyone guide me that how to consume API using Golang with user name and password?

This is the API address


https://emea.universal-api.pp.travelport.com/B2BGateway/connect/uAPI/AirService

if have to pass the following with request

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("username" + ":" + "password")));
             

request.ContentType = "text/xml; encoding='utf-8'";
               

request.Method = "Post";
             

request.Headers.Add("SOAPAction", "");
            

request.Host = "twsprofiler.travelport.com";



I shall be very thankful to him.

Brian Candler

unread,
Jun 19, 2021, 1:15:54 PM6/19/21
to golang-nuts
You don't need to build and encode the Authorization header yourself - you can use Request.SetBasicAuth.  Building it yourself is fine too, if you do it right - google "go http authorization basic" for some examples.

Your code has a lot of other problems:
- you shouldn't put semicolons at the ends of lines
- there is no ContentType member in the Request structure - this is a header.  It's normally passed as an argument to Post.  See the code for client.Post to see how to do it by hand
- similarly, the request method is implied by those functions, or passed as an argument to NewRequest.
- you seem to be overriding the Host from the url with a different one
Reply all
Reply to author
Forward
0 new messages