I want to use the local API of Nature Remo in swift.However, the status code 400 is displayed.

Asked 2 years ago, Updated 2 years ago, 37 views

I'm writing a program because I want to use Nature Remo's local API, but it doesn't work.
I wrote a program to send URL requests while referring to articles on the Internet.I was able to use the API on my Mac terminal, so I'm busy trying to use it as a swift.

You can use the API at the terminal by doing the following:
(AAAAA part is the device ID, XXX is a number)
I also tried ATS for http communication, but it didn't work.

The status code is 400 so I wonder if the content of the request is bad?
I'm almost a beginner who doesn't know right or left, so I'd appreciate it if someone could tell me.

commands executed:

 curl-X POST 'http://AAAA/messages'-H'X-Requested-With: curl'-H' accept: application/json'-d' { "format": "us", "freq":BB, "data": [XXX,XXX, XXXX, XXXX, ... ]}'

Swift code:

import UIKit
    
classViewController:UIViewController {
    
    override func viewDidLoad(){
        super.viewDidLoad()
    }
    
    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }
    
    @IBOutlet weak var label: UILabel!
    @ IBAction func button(_sender:Any){
        
        // create the url-request
        let url = URL (string: "http://XXXXXX/messages")!
        var request = URLRequest(url:url)
        
        // set the method
        request.httpMethod="POST"
        // add the header
        request.setValue("X-Requested-With:curl", for HTTPHeaderField:"Expect")
        request.setValue("accept:application/json", for HTTPHeaderField:"Content-Type")
        
        // set the request-body(JSON)
        let json:[String:Any] = [
            "format":"us",
            "freq":"38",
            "data":[8918,4554,473,657,476,654,500,631,504,627,499,637,503,1739,530,590,481,1762,494,1741,499,1744,494,1738,509,614,522,1720,478,1762,500,639,469,1763,498,1738,471,655,478,661,506,1721,547,586,479,1765,470,1771,494,1738,472,665,466,1768,469,1767,553,574,476,1767,470,660,499,632,474,675,455,39790,553,199,329,192,204,189,174,226,171,227,184,205,251,198,170,195,174,197,171,199,147,196,194,199,171,198,197,171,168,252,189,233,161,208,172,200,170,192,200,197,170,200,169,2464,351
            ],
        ]
        
        //add the "json" as a top level ovject's fact
        request.httpBody = try? JSONSerialization.data(withJSONObject: json)
        
        // use URLSessionDataTask
        let session = URLSession.shared
        session.dataTask(with: request) {(data, response, error) in
            if error == nil, let data = data, let response = response as? HTTPURLResponse {
                
                //   HTTP (Hyper Text Transfer Protocol) header of an acquisition
                print("Content-Type: \(response.allHeaderFields["Content-Type"] ?? "")")
                //   the HTTP status codes
                print("statusCode: \(response.statusCode)")
                print(String(data:data, encoding:String.Encoding.utf8)???")
            }
        }.resume()
        
    }
    
}

swift api

2022-09-30 14:54

2 Answers

I'm not sure if it's directly related because it's not mentioned, but I think Apple's application requires a security setting of ATS for http communication, only https:// communication is allowed, and http:// communication is not allowed.
Check the debug area for ATS warnings when executing the source code.
To avoid this, the application's project file is located in Info.plist to

<key>NSAppTransportSecurity</key>
    <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>AAAAAA/key>
            <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

I think it is necessary to write something like this, but do you have this setting?
Because this example may be incorrect, you must check ATShttpAllow to set the correct value.


2022-09-30 14:54

айка さんThe comment to Mr./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms./Ms.

There is no Nature Remo in kind, so I can only say that it is a possibility considering the content of your questions and comments, but your Swift code now sends a header different from the one curl sends with API enabled, so please correct it first.

If you specify -H'X-Requested-With:curl' in curl, the header field name is X-Requested-With, and its value is curl.

Therefore, the Swift code to send the header to match your curl command is as follows:

//add the header
        request.setValue("curl", for HTTPHeaderField: "X-Requested-With")
        request.setValue("application/json", for HTTPHeaderField: "accept")

I'm sorry that I can't say for sure because I can't try it on hand, but of course, I wonder if the ATS setting written by айка さん is correct or if http://AAAA/messages and in Swift code are the same URL (AAAA and XXXXX>).


2022-09-30 14:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.