I need some suggestion on how to send a POST request using the http module in Flutter with some parameters.
I need to set the username to a string(in the body of the request), and also need to set a property to a FILE in the body.
The easiest way to do requests on Flutter is to use the Dio package
if your json payload is,
{"username":"johndoe", "image":"base64 image data"}
In dio the code looks like
import "dart:io";
import "dart:convert";
import 'package:dio/dio.dart';
// read image bytes from disk as a list
List<int> imageBytes = File("./image.png").readAsBytesSync();
// convert that list to a string & encode the as base64 files
String imageString = base64Encode(imageBytes);
// Send a post request to server
dio.post("/url-to-post-to", data: {"username":"johndoe", "image":imageString});
Related
I need to make a request to my firebase rtdb from my shelf server hosted on 127.0.0.1, I have the url and the db secrets. But whenever i try to make a get request to the db url using the http package, i get a 401 error.
My code:
import 'dart:io';
import 'package:http/http.dart';
import 'package:firebase/firebase_io.dart';
class FirebaseLocalClient {
void putSudokuBoard() async {
var a = await get(
Uri.parse(
"<db url>"),
headers: {
"Authorization": "Bearer <your database secret>",
'Content-Type': "application/js"
});
print(a.statusCode);
//print(a.runtimeType);
}
}
void main(List<String> args) {
FirebaseLocalClient().putSudokuBoard();
}
I call this code from a shelf server(similar to the code in main function), but running it here itself recieves a 401 error.
I am not able to understand why i am recieving a 401 error, i have the db secrets and yet i am unable to get the data at that location. I tried using the admin sdk json but recieved 401 on that too
The output when i use a.body:
The output when i use a.statuscode:
If you are using the db secrets, it looks like you need to append the auth param.
per https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-uri-params
curl 'https://docs-examples.firebaseio.com/auth-example.json?auth=CREDENTIAL'
Remove the Authorization header and try it in curl
I want to send a POST Request where -
Content Type is "multipart / form-data".
In "Body" section, I have 2 params -> body - {static JSON Payload}, files - {any file, say .log file}
In Rest Assured Code, I am able to get the Static JSON Payload in String format with below code -
String jsonFilePath = "<<Path to JSON File>>/Test_new.json";
String response = given().log().all().header("X-AUTH-TOKEN",res).body(new String(Files.readAllBytes(Paths.get(jsonFilePath)))).
when().post("<<POST RESOURCE URL>>").
then().log().body().assertThat().statusCode(200).extract().response().asString();
When running this code, only with Static JSON Payload, I am getting "415" error code.
Questions -
How can we successfully make this kind of call in Rest Assured?
When I want to upload files as well with this call, how to do that?
You need to use multiPart() methods for uploading files, not body() method. For example:
File json = new File("src/test/resources/test_new.json");
File file = new File("src/test/resources/debug.log");
given().log().all()
.multiPart("files", file)
.multiPart("body", json, "application/json")
.post("your_url");
I want to return image/PNG as response to calling URL. How can I do it in Spray?
This may help:
import java.io._
import spray.http._
import MediaTypes._
sender ! HttpResponse(entity = HttpEntity(`image/png`, HttpData(new File("my.png"))))
See also, HttpMessage, HttpEntity, HttpData and MediaTypes. You can do same for HttpRequest as well. You can use Array[Byte] or ByteString instead of File. Checked for Spray 1.3.x.
Following is what I want to implement using JMeter: I want to make a request to an API that implements OAuth signing. The API makes a POST request with a binary file.
I am trying to use OAuth Request sampler plugin. This plugin, unlike HTTP Request sampler, doesn't have 'Send Files with the Request' option.
Is there some way I can still implement it?
To people who might not be familiar with jmeter. Here is sample code.
Add a beanshell sampler and write in Java to sign the request and inject the authorization header into the http request sampler afterwards.
Here is the code for beanshell sampler
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.client.methods.HttpPost;
log.info("start of signing the request");
String consumerKey = "[consumerKey]";
String consumerSecret ="[consumerSecret]";
String token = "[token]";
String secret = "[secret]";
OAuthConsumer consumer;
consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(token, secret);
HttpPost request = new HttpPost("[url]");
consumer.sign(request);
System.out.println(request.getFirstHeader("Authorization").toString());
String oauth = request.getFirstHeader("Authorization").toString().substring(15);
vars.put("oauth" ,oauth);
return oauth;
Did you try authenticating first using OAuth sampler then sending the file using regular http sampler.
I am trying to have a server respond to a request which needs a XML structure. The easiest way I thought would be to create a POST with a string containing the XML, using the Play Framework.
However, I cannot seem to get it to work. I am calling the test with the following code:
Map<String,String> map = new HashMap<String,String>();
map.put("data", xmlString);
Http.Response response = POST("/server/", map);
When on the server checking the parameters it is not in it as it returns false:
params._contains("data")