0

My API accept only Single Quote wrapped JSON. Like:

'{"api_key":"key_api1234","api_secret":"asdfg","uniqueid":"LDM23564GQQP","password":"test1","pin":"94729"}'

I could find a definitive answer neither here nor on the internet.

I Tried many syntax changes on JSON.

let parameters = ["api_key": "key_api1234",
                      "api_secret": "asdfg",
                      "uniqueid": "LDM23564GQQP",
                      "password": "test1",
                      "pin": "94729"]

    guard let url = URL(string: "https://dev-api.authenticateuser") else { return }
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
    request.httpBody = httpBody

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

        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            } catch {
                print(error)
            }
        }

        }.resume()

I am receiving a 500 Error

Inaldor
  • 23
  • 1
  • 4

2 Answers2

0

Inserted Single Quotes inside the String and transform it to Data.

Problem solved:

    guard let url = URL(string: "https://dev-api.authenticateuser") else { return }
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("*/*", forHTTPHeaderField: "Accept")
    let jsonData = "'{\"api_secret\":\"asdfg\",\"uniqueid\":\"LDM23564GQQP\",\"pin\":\"94729\",\"password\":\"test1\",\"api_key\":\"key_api1234\"}'".data(using: .utf8)

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

        if let data = data {
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            } catch {
                print(error)
            }
        }

    }.resume()
Inaldor
  • 23
  • 1
  • 4
0

That's a bad design API. Forcing to embed the JSON into single quotes, I'd suggest if you cant to tell the back-end developper to fix it.

Still, if you want it to work, you can just convert single quote to Data, and what you called httpBody, append that single quote before and after.

With bad practice with force unwrap (use of !), but to emphasize the logic:

let parameters = ["api_key": "key_api1234",
                  "api_secret": "asdfg",
                  "uniqueid": "LDM23564GQQP",
                  "password": "test1",
                  "pin": "94729"]

var request = URLRequest(url: URL(string: "www.stackoverflow.com")!)

let singleQuote = "'".data(using: .utf8)!
let parametersJSON = try! JSONSerialization.data(withJSONObject: parameters, options: [])

let finalBody = singleQuote + parametersJSON + singleQuote
print("request.httpBody string: \(String(data: finalBody, encoding: .utf8)!)")
request.httpBody = finalBody

Output:

$>request.httpBody string: '{"api_key":"key_api1234","uniqueid":"LDM23564GQQP","pin":"94729","password":"test1","api_secret":"asdfg"}'
Larme
  • 24,190
  • 6
  • 51
  • 81