hkblue
December 11, 2021, 3:09am
1
Hi, I tried to create a http request manually in Swift. Here is my code:
let url = URL(string: "http://localhost:1337/parse/test")!
let requestUrl = url // Prepare URL Request Object
var request = URLRequest(url: requestUrl)
request.setValue("application-idValue", forHTTPHeaderField: "X-Parse-Application-Id")
request.setValue("secret-keyValue", forHTTPHeaderField: "b10bb5d0587e4518a20031605636ab85")
request.httpMethod = "POST"
But, Xcode says “Connection refused”. Is my request header setting correct?
Thanks
hkblue
December 13, 2021, 12:56am
2
Hi, I changed
request.setValue("application-idValue", forHTTPHeaderField: "X-Parse-Application-Id")
request.setValue("secret-keyValue", forHTTPHeaderField: "b10bb5d0587e4518a20031605636ab85")
request.setValue("b10bb5d0587e4518a20031605636ab85", forHTTPHeaderField: "X-Parse-Application-Id")
.
It works because it can bring the json data back from the server. However, it also generate some errors:
**SwiftUI_practice[95348:133487072] [connection] nw_socket_handle_socket_event [C2.1:2] Socket SO_ERROR [61: Connection refused]**
**Response data string:**
**{"tokenA":123455}**
Any idea why? Thanks
cbaker6
December 13, 2021, 1:03am
3
The easiest way to use Swift with Parse is using the Swift SDK as it uses REST in the necessary ways to connect to a Parse Server.
The Swift SDK for Parse Platform (iOS, macOS, watchOS, tvOS, Linux, Android, Windows)
If you insist on writing your own API (this will not be easy) then you can look for guidance in:
//
// API+Command.swift
// ParseSwift
//
// Created by Florent Vilmart on 17-09-24.
// Copyright © 2020 Parse Community. All rights reserved.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
internal extension API {
// MARK: API.Command
struct Command<T, U>: Encodable where T: ParseEncodable {
typealias ReturnType = U // swiftlint:disable:this nesting
let method: API.Method
let path: API.Endpoint
let body: T?
This file has been truncated. show original
//
// API.swift
// ParseSwift
//
// Created by Florent Vilmart on 17-08-19.
// Copyright © 2017 Parse. All rights reserved.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
// swiftlint:disable line_length
/// The REST API for communicating with a Parse Server.
public struct API {
internal enum Method: String, Encodable {
case GET, POST, PUT, PATCH, DELETE
This file has been truncated. show original
hkblue
December 14, 2021, 4:38pm
4
@cbaker6 Thanks. It is not exactly what I asked for. But it is definitely useful to check the documents.
When I am fetching data without using the SDK, that is via the REST services I set these header values:
request.addValue(“application/json”, forHTTPHeaderField: “Content-Type”)
request.addValue(myRestValue, forHTTPHeaderField: “X-Parse-REST-API-Key”)
request.addValue(myApplicationID, forHTTPHeaderField: “X-Parse-Application-Id”)
Hope this helps.
Halldor