Hello I'm trying to translate POST request api code written in php:
<?php
$methodParams = '{
"date_confirmed_from": 1407341754,
"get_unconfirmed_orders": false
}';
$apiParams = [
"method" => "getOrders",
"parameters" => $methodParams
];
$curl = curl_init("https://api.baselinker.com/connector.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: xxx"]);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
$response = curl_exec($curl);
To Swift but I don't have idea why it doesn't work:
import UIKit
struct Parameters: Codable {
let date_confirmed_from: Int
let get_unconfirmed_orders: Bool
}
struct BodyData: Codable {
let method: String
let parameters:Parameters
}
let url = URL(string: "https://api.baselinker.com/connector.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.allHTTPHeaderFields = [
"X-BLToken": "xxx"
]
let bodyData = BodyData(method: "getOrders", parameters: Parameters(date_confirmed_from: 1407341754, get_unconfirmed_orders: false))
let encoder = JSONEncoder()
do {
let encodeData = try encoder.encode(bodyData)
request.httpBody = encodeData
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error)
}
if let data = data {
}
}
task.resume()
}catch {
print(error)
}
When I'm testing my Swift code I'm getting error answer from API can someone help me to find fault in my code?
Does the PHP curl work?
It is not what the baseline documentation is asking for.
The documentation asks for this post data:
array (
'method' => 'getOrders',
'parameters' => '{"date_from": 1407341754}',
)
Your PHP curl request post data:
'array (
'method' => 'getOrders',
'parameters' => '{
"date_confirmed_from": 1407341754,
"get_unconfirmed_orders": false
}',
)'
The documentation asks
parameters = arguments of the requested function in JSON format.
Your parameters are an array, not JSON.
Okay I find out solution for this problem:
import UIKit
let url = URL(string: "https://api.baselinker.com/connector.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Accept")
request.setValue("xxx",forHTTPHeaderField: "X-BLToken")
let data: Data = "method=getOrders¶meters={\"date_confirmed_from\": 1407341754&\"get_unconfirmed_orders\":false}".data(using: .utf8)!
request.httpBody = data
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error)
}
if let data = data {
let dataString = String(data: data, encoding: .utf8)
print(dataString!)
}
}
task.resume()
The biggest problem was coding of data. curl in PHP by default sending data as x-www-form-urlencoded
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
UIkit is another popular frontend wrapper that is arguably a little underrated in terms of CSS capabilities. In addition to many features similar to those found on other popular platforms, there are several useful specialized components.
https://getuikit.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.