'Internal Server Error - 500. Object reference not set to an instance of an object. step 0
I am making a POST API call from my swiftUI code. I have made sure my API call code is working in SwiftUI and I tested that with other urls. The issue is the same API call works from my NativeScript Angular code but it doesn't work from my swiftUI code and throws this following error.
Internal Server Error - 500. Object reference not set to an instance of an object. step 0
This is how I am making a request in NativeScript Angular which works fine.
let url = "some_url/signIn?email=${userName}&password=${password}"
const httpOptions = {
headers: new HttpHeaders({ "Content-Type": "application/json" }),
};
this._http.post<User>(url, httpOptions).subscribe((results: any) => { /* data processing */ } )
Where this._http is injected in the constructor.
Following is how I am making the API call in swift which throws an error.
guard let url = URL(string: "some_url/signIn?email=\(userName)&password=\(password)") else {fatalError("Missing URL")}
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
let (data, response) = try await URLSession.shared.data(for: urlRequest)
guard(response as? HTTPURLResponse)?.statusCode == 200 else {fatalError("Error while fetching data")}
This last line is where the error occurs.
I have never seen this error before and can't find any good results.
Any kind of information would be helpful.
Thank you in advance :)
Solution 1:[1]
I figured out the issue,
I just became aware that I was passing headers in my NativeScript Angular code, but not in my SwiftUI code.
I just simply added this line and the API call on SwiftUI works fine now.
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prabhas Kumra |
