Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a curl request in swift?

Tags:

curl

swift

api

I am trying to use an external API, which has an API request that is like the following. I am used to requests with just one url, but what do I do with the "H" and the "d" arguments? Should I include them in my url or

$ curl -X POST https://api.lucidtech.ai/v0/receipts \
  -H 'x-api-key: <your api key>' \
  -H 'Content-Type: application/json' \
  -d '{"documentId": "a50920e1-214b-4c46-9137-2c03f96aad56"}'

Currently I have the following code, but where do I place the API key and the document id in this code?

@IBAction func getScannedData(_ sender: Any){
    guard let url = URL(string: "https://api.lucidtech.ai/v0/receipts") else {return}

    let session = URLSession.shared
    session.dataTask(with: url) { (data, response, error) in
        if let response = response{
            print(response)
        }


}
like image 468
Arnav GUPTA Avatar asked Aug 27 '18 03:08

Arnav GUPTA


People also ask

How do I run a cURL request?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.

How does a cURL request work?

The client, curl, sends an HTTP request. The request contains a method (like GET, POST, HEAD etc), a number of request headers and sometimes a request body. The HTTP server responds with a status line (indicating if things went well), response headers and most often also a response body.

What is cURL HTTP request?

cURL: It stands for “client URL” and is used in command line or scripts to transfer data. It is a great tool for dealing with HTTP requests like GET, POST, PUT, DELETE, etc.

What is cURL API request?

cURL is a command-line tool for getting or sending files using URL syntax. Since cURL uses libcurl, it supports the same range of common Internet protocols that libcurl does. This tutorial will walk through the format and syntax used when making Oracle Eloqua API requests with cURL.


1 Answers

This is an example of how you can translate the curl command into URLRequest:

guard let url = URL(string: "https://api.lucidtech.ai/v0/receipts"),
    let payload = "{\"documentId\": \"a50920e1-214b-4c46-9137-2c03f96aad56\"}".data(using: .utf8) else
{
    return
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("your_api_key", forHTTPHeaderField: "x-api-key")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = payload

URLSession.shared.dataTask(with: request) { (data, response, error) in
    guard error == nil else { print(error!.localizedDescription); return }
    guard let data = data else { print("Empty data"); return }

    if let str = String(data: data, encoding: .utf8) {
        print(str)
    }
}.resume()
like image 168
Code Different Avatar answered Sep 18 '22 17:09

Code Different