-2

I understand this is not conventional way but i am bound by client.

what is the cUrl equivalent to

Expected

curl -v -X POST -H "Authorization: Basic XXXX==" -d Rules='[{\"some\":\"thing\"}]' "xyz.com/rules"

Focusing on a Rules Field inside -d

Using

struct Parameters: Encodable {
    let Rules: [ [String : String] ]
}

Session().request(
    "xyz.com",
    method: .post,
    parameters: Parameters(),
    encoder: JSONEncoder()
).cURLDescription { description in
    print("cURLDescription: ", description)
}

This will result into

Present

curl -v -X POST -H "Authorization: Basic XXXX==" -d '{"Rules":"[{\"some\": \"thing\"}]}' "xyz.com/rules"

But this is not what is expected, need something which produces the above Expected cUrl command

Abhinava B N
  • 165
  • 1
  • 12
  • `Rules='[{\"some\":\"thing\"}]'`: That's not JSON. Instead, create yourself a parser that will generate it from `Parameters`. Then you can see https://stackoverflow.com/questions/27855319/post-request-with-a-simple-string-in-body-with-alamofire – Larme Jun 16 '21 at 16:05

1 Answers1

0
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

var semaphore = DispatchSemaphore (value: 0)

var postData: Data? = nil
if let data = "Rules={\"some\":\"thing\"}".data(using: .utf8) {
    postData = Data(base64Encoded: data)
}
var request = URLRequest(url: URL(string: "xyz.com/rules")!,timeoutInterval: Double.infinity)
request.addValue("Basic XXXX==", forHTTPHeaderField: "Authorization")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
  guard let data = data else {
    print(String(describing: error))
    semaphore.signal()
    return
  }
  print(String(data: data, encoding: .utf8)!)
  semaphore.signal()
}

task.resume()
semaphore.wait()

Use this extension to print curlString. https://gist.github.com/shaps80/ba6a1e2d477af0383e8f19b87f53661d

RTXGamer
  • 3,215
  • 6
  • 20
  • 29