Comments (7)
Data as string looks like this:
"["https:\/\/img.motofotky.cz\/upload\/images\/forum\/2020\/12\/516270_upload.jpg"]"
I am able to parse it perfectly like this:
func jsonValue() -> Any? {
if let data = data(using: .utf8) {
return try? JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .allowFragments])
}
return nil
}
And I get swift array with one string obviously. So I believe parsing in you case is more strict what is in this case not useful at all as we developers sometimes don't have control over server responses or it is so complicated it's best to avoid...
from networking.
So I modified your library but, I just made small change...
public class JSONResponse: Response {
let json: JSON
public let body: Any?
public var dictionaryBody: [String: Any] {
return json.dictionary
}
public var arrayBody: [[String: Any]] {
return json.array
}
public var data: Data {
switch json {
case let .array(value, _):
return value
case let .dictionary(value, _):
return value
case .none:
return Data()
}
}
init(json: JSON, response: HTTPURLResponse, body: Any? = nil) {
self.json = json
self.body = body
super.init(response: response)
}
}
if let finalError = returnedError {
self = .failure(FailureJSONResponse(json: json, response: response, error: finalError))
} else {
self = .success(SuccessJSONResponse(json: json, response: response, body: body))
}
But I don't believe you want to push this up. Maybe better fix that parser please.
from networking.
Data as string looks like this:
"["https://img.motofotky.cz/upload/images/forum/2020/12/516270_upload.jpg"]"
@rene-dohan, Hi, could you post the output of the curl request to the API endpoint here? Just the copy/paste from terminal window. It seems that there is no need to make changes to the library code, you just need to change the parsing method that you use. But this needs to be clarified.
from networking.
@rene-dohan, I looked more closely and, as I understand the essence of the problem, you need access to the raw data, but the library does not allow this.
But the library allows. Look at this:
networking.get("/endpoint") { result in
switch result {
case .success(let response):
let jsonData = response.data
do {
let array = try JSONDecoder().decode([String].self, from: jsonData)
print(array as Any)
} catch {
print(error.localizedDescription)
}
case .failure(let response):
let json = response.dictionaryBody
print(json)
}
}
This snippet does just what you need. The result is an array of string values.
from networking.
No becase I see empty data there :) look at your source code please ;)
public var data: Data {
switch json {
case let .array(value, _):
return value
case let .dictionary(value, _):
return value
case .none:
return Data()
}
}
So basically you return empty Data(), I don't know why , what should I do with it ? :) well its probably because you think .none means there was not data in body but as I say youre json parsing failed and in such situation you BLOCKED ACCESS to VALID response body.
I already successfully modified your lib as I told you, to get to body of response but you should be aware of this issue.. It could be quite good library if you make it more flexible, or just make that parsing to return that array :)
from networking.
@rene-dohan I am not the author of this library. I just wanted to help. :)
So basically you return empty Data(), I don't know why
In order not to return nil, of course.
from networking.
@rene-dohan I conducted a series of experiments and ended up with the fact that we really do not have access to raw data. So you are right.
Could you make a pull request?
from networking.
Related Issues (20)
- Support for JSONDecoder HOT 8
- Token Refresh HOT 6
- Json parameters support on DELETE HOT 3
- Progress Multipart Upload HOT 2
- Support PATCH method HOT 1
- Network Activity Indicator not showing
- Change the base URL for a single request? HOT 1
- Installation Support Swift Package Manager HOT 2
- Caching folder should not be documents folder HOT 1
- fakeGET crashes under XCode 10.0 and Swift 4.2
- Expose handleJSONRequest(..)? HOT 3
- Problem with changes on UI Threath HOT 3
- `cacheOrPurgeData` causing production crashes HOT 3
- Can't use Swift 5 Result enum due to interference with custom one. Need to rename? HOT 3
- Facing a strange crash time to time HOT 3
- No waitsForConnectivity support for background request. HOT 1
- Using Networking lib to make a post in the background? HOT 3
- Question - Request caching HOT 2
- Troubleshooting Logging Errors in Xcode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from networking.