Does anyone know how to extract html response from Guzzle HTTP client? If you look at the example below, we can get xml and json response easily but I don't know how to get plain text or HTML response string.
Documentation didn't have an option for plain text or HTML unlike json() and xml().
$client = new Client($base_url);
$request = $client->createRequest($method, $uri, null, $this->requestPayload);
$response = $client->send($request);
$xml = $response->xml(); // For XML response
$json = $response->json(); // For JSON response
$html = $response->????????(); // For plain text or HTML response
Solution:
This returns the whole response body as we see in browser.
$response->getBody(true)
Related
I need to post a request from JSON file in my selenium test. The below code is showing response code as 400.
Manually tested, it's working fine and response code is 200 for the same json body.
When this json body is stored in a json file and executed the below code, then response code displays as 400.
String fileName = "D:/json/test1.json";
String json = FileUtils.readFileToString(new File(fileName), "utf-8");
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); // required url is already saved in 'url' variable.
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/JSON");
con.setRequestProperty("Accept", "application/JSON");
con.setRequestProperty("Accept-Charset", "UTF-8");
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
byte[] response = json.getBytes("utf-8");
out.write(response.toString());
System.out.println(httpConn.getResponseCode());
Please, appreciate any help on this?
I followed these steps:
Registered a bot with BotFather.
Send a post request for the webhook (https://api.telegram.org/bot[BOTID]/setWebhook) with the URL being https://example.com/myscript.php
Double check with getWebhookInfo and it showed it is correctly registered.
When I send a message to the bot, the script is being called but with an empty POST payload. In the documentation they say they would send an HTTPS POST request to the specified url, containing a JSON-serialized Update.
Does anyone else has this issue and perhaps know a way to resolve this?
My php script to log:
$file = dirname(__FILE__) . '/telegram-log.txt';
$entry = (object)array();
$entry->date = date("r");
$entry->GET = $_GET;
$entry->POST = $_POST;
$entry->REQUEST = $_REQUEST;
$entry->HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
$entry->REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
file_put_contents($file, json_encode($entry) . "\n", FILE_APPEND | LOCK_EX);
Response:
{"date":"Thu, 17 Jun 2021 13:42:49 +0200","GET":[],"POST":[],"REQUEST":[],"HTTP_USER_AGENT":null,"REMOTE_ADDR":"91.108.6.133"}
Use $content = file_get_contents("php://input") to receive updates.
Telegram's response is of content-type application/json.
$_POST will only work when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
I have been using RESTSHARP automating REST API test cases. So far I am familiar in using JObject like:
public IRestResponse HELPERS_POST(JObject input)
{
...
request.RequestFormat = DataFormat.Json;
request.AddParameter("text/json", input, ParameterType.RequestBody);
...
My problem now is I have a new API with the following information:
Parameters: nodeIds Parameter
Type: body
Data Type: Array[long] or as example {[12345,23453,45332]}
I parsed the above as JArray.
Now I don't know what to do with this JArray so my HELPERS_POST() can use it to make a POST request.
I'm trying to send a PDF for content extraction to a Tika Server but always get the error: "Cannot convert text from stream using the source encoding"
This is how Tika is expecting the files:
"All services that take files use HTTP "PUT" requests. When "PUT" is used, the original file must be sent in request body without any additional encoding (do not use multipart/form-data or other containers)." Source https://wiki.apache.org/tika/TikaJAXRS#Services
What is the correct way of sendig the file with XMLHttpRequest()?
Code:
var response, error, file, blob, xhr;
file = new File("/PROJECT/web/dateien/ai/pdf.pdf");
blob = file.toBuffer().toBlob("application/pdf");
url = "http://localhost:9998/tika";
// send data
try {
xhr = new XMLHttpRequest();
xhr.open("PUT", url);
xhr.setRequestHeader("Accept", "text/plain");
xhr.send(blob);
} catch (e) {
error = e;
}
({
response: xhr.responseText,
status: xhr.statusText,
error: error,
type: xhr.responseType,
blob: blob
});
Error:
I suspect PUT request to be converted into a POST request by wakanda when there is blob in XHR body. Can you wireshark your XHR request and add details ? If so, you can probably fill an issue in wakanda (https://github.com/Wakanda/wakanda-issues/issues)
Hope it helps,
Yann
I'm writing a web API that has a post method accepting files uploaded from UI.
public async Task<List<string>> PostAsync()
{
if (Request.Content.IsMimeMultipartContent("form-data"))
{
string uploadPath = HttpContext.Current.Server.MapPath("~/uploads");
var streamProvider = new MyStreamProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(streamProvider);
return streamProvider.FileData
.Select(file => new FileInfo(file.LocalFileName))
.Select(fi => "File uploaded as " + fi.FullName + " (" + fi.Length + " bytes)")
.ToList();
}
else
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!");
throw new HttpResponseException(response);
}
}
Then I post a request for the above action by postman.
I set the content-type header to multipart/form-data
but an error occurred during the execution of action.
here is the error message body :
"Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.\r\nParameter name: content"
I went to the postman headers but I found that the request header content type was set to application-json.
You are looking on the response header which is json format and this is ok for you.
Your real problem is with the postman request, so just remove the 'Content-Type: multipart/form-data' entry from request header.
It's enough to upload a file as form-data and send the request.
Look what happen when you set the Content-Type manually vs. when you not:
Postman knows to set both the content type and boundary, since you set only the content type
First: Postman have a bug in handling file-based requests.
You can try adding this to your WebApiConfig.cs it worked for me:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();