How to read the headers of the REST API response using TRON and Alamofire - xcode8

I am currently using Xcode8 and have built an app that make REST API calls using TRON and Alamofire.
I am successfully calling the API and getting back a response. This response BODY is being parsed into a model class that is structured to mimic the response body of the API call, and all of my properties are being populated with the correct values.
The new requirement I have now been given is to read some authentication information from the HEADER of the response. This information has been defined as a JSON Web Token. I can't seem to find any information on how to parse this information from the response HEADER using TRON or Alamofire.
Any insight, example, links, or comments will be greatly appreciated. Thanks in advance for any help.

I found the answer on this SO post.
And then I solved it in my project like this:
if let authorization = response.response?.allHeaderFields["Authorization"] as? String {
UserDefaults.standard.set(authorization, forKey: Constant.AUTHORIZATION_TOKEN)
}

Related

Salesforce ServiceM8 Integration

We had already integrated Salesforce and ServiceM8. We have been pushing a custom object into servicem8 and creating a job.
This was working well before but not for last few days. We get the below error while posting.
Error:
{"errorCode":400,"message":"Bad Request. No data received in POST"}
Can you please identify what does this mean?
NOTE: We have been sending this in JSON format. We tried with the example that ServiceM8 given in the API document. That itself didn't get posted now.
Please assist, if there is any changes in the serviceM8 api.
I am passing the content type through header. Please find the sample request and response below.
Request===System.HttpRequest[Endpoint=https://api.servicem8.com/api_1.0/job/0bd2a066-8ac8-4d0c-83db-2deb78973ceb.json, Method=POST]
JSON String==={"status":"Completed","purchase_order_number":null,"job_description":"","job_address":"test, test, test, test, test","customfield_strata_number":"Yes","customfield_service_sheet_url":"SS2000420","customfield_salesforce_id":"012038","customfield_purchase_order_number":null,"customfield_notes_to_service":null,"customfield_notes_from_previous_service":null,"customfield_message_to_technician":null,"customfield_booking_notes":null,"customfield_booked_with_customer":"Yes","company_uuid":"15792888-b2f4-4538-a325-c3a58defaddb","badges":"[\"64cb190d-6a08-4ce2-ab08-56d8999c8a8b\",\"2aec7e2e-7370-42d0-9ea2-f16a859a2d1b\",\"a225a59f-b21f-4ea1-bce7-5e28ff2bd89b\",\"e71de9ce-8207-4bdd-a178-f05f35c675cb\",\"62795263-6867-45d8-868f-ab8cfbcf897b\",\"08b0cfa0-2d9c-438e-b7b0-ba41a437607b\",]"}
Response:
VF_PAGE_MESSAGE|{"errorCode":400,"message":"Bad Request. No data received in POST"}

How to send post request using Spookyjs with body parameter?

I want to send a post request with JSON body to a particular server using Spookyjs. How I do that please help into this?
If you are using spooky, then you are using nodeJS. Gather your informations (which I'm guessing you want to do) with spooky / casper, pass them back to nodeJS, parse your data and post it.

Captcha required youtube api C#

Every time I make a request for getting a video to youtube API I make something like that:
public Video GetVideo(string videoId)
{
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + videoId);
return request.Retrieve<Video>(videoEntryUrl);
}
Sometimes I get an exception saying "Captcha required". I was wondering if building the YoutubeRequest is asking for an authentication token for every call to GetVideo and because of that I'm getting this exception. Is it possible? How can I avoid this exception? And I'm not talking about handling it with a try-catch.
Thanks!!
Yes; there are ways of reusing a ClientLogin token. Please see scenario 4 in this blog post, and take a look at the "Recalling an auth token" section of this document.
Better yet, I'd recommend making the move to OAuth 2 instead of ClientLogin, as mentioned in that blog post.

How to format an HTTP PUT request?

I am using the Yahoo Placemaker API and would like to send a request but I am getting confused as to how to send the request. The request is composed of a URL and a document and inside the document, there are a bunch of parameters. Please see below.
http://wherein.yahooapis.com/v1/document
documentContent=Sunnyvale+CA
documentType=text/plain
appid=my_appid
How do I format the URL into a request is it like so?
http://wherein.yahooapis.com/v1/documentContent=Sunnyvale+CA?documentType=text/plain?appid=my_appid
I would like to use this the Placemaker service for a Mac app written in objective-c.
Any suggestions would be great. Thanks.
You don't. The URI and the request body are different things.
It would be helpful if you explained why you're asking. What platform/API/language are you using?

POSTing to web service API in Objective C?

I'm writing one of my first apps for consuming a web service in Objective C, it's a Lighthouse API client. I'm able to execute all the GETs and XML parsing correctly and quickly, but I'm having extreme trouble trying to create a new ticket via POST (http://lighthouseapp.com/api).
I'm using ASIHTTPRequest.
I tried including the parameters on the URL (i.e. POST /projects/#{project_id}/tickets.xml?title=boo).
I've tried putting the ticket XML in the request body.
<ticket><title>boo</title></ticket>
Nothing is working. (server always sends a response back saying it needs a title) I'm very new to web services - am I missing something obvious?
I had a quick look at the Lighthouse API and here's how you go about creating a new ticket.
Request URL is http://{yourCustomURL}.lighthouseapp.com/projects/{ProjectID}/tickets.xml where {ProjectID} is a 5 digit number - in my case 72945.
Method is POST
Content type should be set to application/xml
Body should be in the format below. All fields are optional so I only included the title
<ticket> <assigned-user-id type="integer"></assigned-user-id> <body></body> <milestone-id type="integer"></milestone-id> <state></state> <title>Testing new ticket creation</title></ticket>
(sorry about the formatting of the code above, SO doesn't seem to like XML formatted code somehow?
This worked for me with a new ticket created under projectID 72945 - response received was 201 Created
If you want to make sure your POST request is working before diving into ASIHTTPRequest, download a Firefox add-on called POSTER from here. This will allow you to send an authenticated post request with all the fields above. Once you get that working, it should be a piece of cake to get ASIHTTPRequest to do the same.