Swift Error "Domain=Nscocoaerrordomain Code=3840 "Invalid Value Around Character 1."

swift error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 1.

Adding .json at the end of the URL endpoint has solved the error. ie https://www.example.com/rest/user/login.json

Error (Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 0. UserInfo={NSDebugDescription=Invalid value around character 0.}

There is an issue with my web service. They are giving me the response in "text/HTML" format rather than HTML. When i printed my response on debugger then i got:

"Content-Type" = "text/html; charset=UTF-8";

Now, i updated my webservice and everything is working like a charm.

Alamofire post request getting **Invalid value around line 1, column 0.**

Below code is working when i removed headers from request.

func apiPostRequest1(parameters:[String:String], url:String,  completionHandler: @escaping (Any?) -> Swift.Void) {

session.request(url,
method: .post,
parameters: parameters,
encoding: URLEncoding.httpBody).validate(statusCode: 200..<600).responseJSON{ response in
switch response.result {
case .success(let JSON):
completionHandler(JSON)
case .failure(let error):
let responseData = String(data: response.data!, encoding: String.Encoding.utf8)
print(responseData ?? "Error in encoding response data")
print("Request failed with error \(error)")
completionHandler(response.response?.statusCode)
}
}
}

Alamofire .post api error: Code=3840 Invalid value around character 1

Replace responseJSON with responseString.

You need to convert the string for values. Can you update the string in your question?

You can use this function to convert response string into JSON:

func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}

String: let str = "{\"name\":\"James\"}"

USAGE: let dict = convertToDictionary(text: str)

How Fix Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 52.

First of all and already mentioned the string format is clearly not JSON.

It's the string format which is returned when calling the description property of a Foundation collection type (NSArray / NSDictionary).

For example a print statement calls description and this format appears also in output of Terminal.app.

However there is a solution: This string format is called openStep (an OpenStep / NeXt legacy format) and is available in PropertyListSerialization

This code reads the format:

let string = "{\n ID = \"d9a7c7bf-781d-47b3-bb4e-e1022ec4ce1b\";\n Name = Headquarters;\n}"
let data = Data(string.utf8)

do {
let dictionary = try PropertyListSerialization.propertyList(from: data, format: nil)
print(dictionary)
} catch { print(error) }

Note:

I'm pretty sure that the original data format is not openStep and somewhere you created the string unnecessarily with the String(describing initializer like in the question.



Related Topics



Leave a reply



Submit