How do I see response text in debugger? - alamofire

If this is my request, how do I see the response's text in the debugger window, in particular when there's an error?
let request = Alamofire.request(
webServiceUrl + url, method: method, parameters: parameters)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
request.responseJSON { response in ... }

In my response handler, I added...
if let resp = response.data {
NSLog("Server Response: \(NSString(data: resp, encoding: String.Encoding.utf8.rawValue))")
}

Related

XMLHttpRequest interception

I want that when some XMLHttpRequest at 'https://myurl.com' be send, isntantly returns status code 200 and myOwnResponse at response body, I don't want that the request go to the server, the reason is that the server response is very slow, and I want to increase the performance.
I want something like tweak: mock and modify HTTP requests, how can I do that?
var _open = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, URL) {
const myUrl = 'https://myurl.com'
if(method === 'GET' && URL === myUrl) {
const myOwnResponse = {'myResponse': true}
return myOwnResponse
} else {
return _open.apply(this, arguments);
}
};

First Alamofire request blocks next alamofire request

//// I get a sucess response from first request in 2 seconds but still it waits for 30 seconds to hit second api
let request = AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
switch response.result {
case .success(let data):
print("data \(data)")
let url = URL(string: "https://apps.com/api/restid?user_id="+Constants().GETVALUE(keyname: USER_ID))!
AF.request(url).responseJSON { response_next in
print(response_next)
}
break
case .failure(let error) :
print("error \(error)")
}
}

instagram ?__a=1 not working in Mobile but when we hit the api in browser and postmen its working fine

It's stopped working for the last 5 days before that it's working fine.
now when we call this end point in mobile then return 429 and redirect to the Instagram login page.
I had the same problem and I've fixed it by changing the User-Agent to Mozilla/5.0 in the request's header.
Here is what I did on iOS (Swift):
let url = URL(string: "https://www.instagram.com/spacex/?__a=1")
let request = NSMutableURLRequest(url: url)
request.setValue("Mozilla/5.0", forHTTPHeaderField: "User-Agent")
request.setValue("no-cache", forHTTPHeaderField: "Cache-Control")
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
DispatchQueue.global(qos: .background).async {
let requestData = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let error = error {
print("Error # loadInstagram(): \(String(describing: error))")
return
}
if let data = data {
self.processInstagram(data: data) // function for processing the json data received
} else {
print("Error - missing data # loadInstagram()")
}
}
requestData.resume()
}

PostMan is working but http is giving 404 error in flutter

I am using following code for my api. This api is working in postman but showing 404 error code in http flutter.
My flutter code:
RaisedButton(
onPressed: () {
apiCall();
},
child: Text("Press"),
)
Future apiCall() async {
var body =
jsonEncode({"filepath": "patient/reports/1602333458533-Liver.jpg"});
try {
await http
.post('http://3.6.197.52:3100/downloadFile',
headers: {"Accept": "Application/json"}, body: body)
.then((http.Response response) => print(response.statusCode));
} catch (e) {
print(e);
}
}
It is giving error code 404 .
Following is postman result:
Post Man result
You are setting the wrong headers. The Accept header is for determining which result type you expect from the server. From your screenshot (and the data) it seems quite clear, you would expect an image/jpg. On the other hand, you are missing the Content-Type header, which defines what type of data you are sending with your request, in your case application/json. So the server probably can't parse the body correctly.
Assuming, that jsonEncode is just something like JSON.stringify you should do something like the following
Future apiCall() async {
var body =
jsonEncode({"filepath": "patient/reports/1602333458533-Liver.jpg"});
try {
await http
.post('http://3.6.197.52:3100/downloadFile',
headers: {"Content-Type": "application/json"}, body: body)
.then((http.Response response) => print(response.statusCode));
} catch (e) {
print(e);
}
}

How to detect http response code?

I use video.js player with HLS.
I would like to detect HTTP response code for each failed request, which video.js makes to play a video. For example, if a response is 304 then my code needs to make a special action (show a message).
player.on('error', ...) does not provide such information. As well as tech level error.
I need something like:
player.on('request', function(response) { ... }
The best solution I found is wrapping video.xhr object with special handler.
Example of the code (using underscore.js library):
videojs.xhr = _.wrap(videojs.xhr, function(fn, options, callback) {
var wrapped_callback = _.wrap(callback, function(cb_fn, error, response) {
var args = _.rest(arguments, 1);
console.log('Error', error);
console.log('Response', response);
var res = cb_fn.apply(this, args);
return res;
});
return fn.apply(this, [options, wrapped_callback]);
});