I have this web service
Url: http://menatservice.menatbb.com/MenaServiceCommon.svc/TestUploadImage
Form Data:
ListId : 10
ListType : 1
MenaImage: >> put any image
I tried
val formData = listOf("ListId" to moduleId, "ListType" to moduleType,
"MenaImage" to listOf{images[0].file.inputStream().readBytes()})
this#UploadImageRequest.data1 = images[0].file.readBytes()
return Constants.TestUploadImage.httpUpload(Method.POST, formData)
but I got 400 bad request error while in Postman it is working as the image below
what should I do to make it work by Fuel library?
Related
i am trying to upload image to parse server
but it return
https://parsefiles.back4app.com/ehUKQVObBspFk0MBFNSSg3MwLJofpeoFtDhQNIgS/2f345485e2e5cc757c80cfdacf138871_hello.png
whitch contains image cant be displayed because it contain error
it success on uploading image but image is corrupted
note : when i use same code to upload pdf or mp3 files it works well
val response = client.post<HttpResponse>("https://encyriptionapp.b4a.io/parse/files/hello.png") {
headers.append("X-Parse-Application-Id", PublicData.Application_Id)
headers.append("X-Parse-REST-API-Key", PublicData.REST_API_Key)
body = MultiPartFormDataContent(
formData {
append("file", file/*bytes*/, Headers.build {
append(HttpHeaders.ContentType, "image/png")
})
}
)
}
I am trying to use theDuckDuck go api with this code
val mainVal = searchQuery.replace(" ", "+")
val url = URL("https://api.duckduckgo.com/?q=$mainVal&format=json")
println(url.host)
url.openConnection()
val json = url.readText()
I am getting this error as a result
java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.duckduckgo.com/?q=duck+duck+go&format=json
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at java.base/java.net.URL.openStream(URL.java:1139)
at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:150)
at com.andromeda.araserver.skills.GetInfo.getFast(GetInfo.kt:82)
at com.andromeda.araserver.skills.GetInfo.searchBing(GetInfo.kt:43)
at com.andromeda.araserver.skills.GetInfo.main(GetInfo.kt:28)
at com.andromeda.araserver.Run.serve(Run.java:98)
at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:945)
at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)
at java.base/java.lang.Thread.run(Thread.java:834)
When I run the given link in my browser it returns the expected result. I tried using the HttpSConnection method, it did not work.
note I have used this methood literly hundreds of times on https, thats not the issue.
Make sure you added INTERNET permission in Manifest
&
Instead adding "+" use Uri.encode method for encoding URLs :
// val mainVal = searchQuery.replace(" ", "+")
val mainVal = Uri.encode(searchQuery)
val url = URL("https://api.duckduckgo.com/?q=$mainVal&format=json")
println(url.host)
url.openConnection()
val json = url.readText()
println("Result = " + json)
I sent image file and content (text) using retrofit from android client and tried to get them in the server side but can not do that and the error is always : it is null.
Please how can I send image and receive it using #Retrofit and Slim framework?
If anyone can help, I will appreciate.
..............................................
Tried to send the image from android client like a multipart file and receive it with slim using method (getUploadedFiles) and it didn't work.
#Multipart
#POST("createPostWithImage")
Call<DefaultResponse> uploadTestPost(
#Part("desc") RequestBody desc,
#Part MultipartBody.Part image
);
$app-> post('/createPostWithImage', function(Request $request, Response $response) {
$directory = __DIR__.'../photos/1';
$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['photo'];
$uploadedFile->moveTo($directory);
});
get the file which was sent from android client and save it into specific folder.
$app->post('/file', function($request,$response) {
try{
$directory = $this->get('upload_directory');
$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['example1'];
$extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$basename = mt_rand(10000000, 99999999);
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $basename.'.'.$extension);
return $response->withJson(array('message' =>$uploadedFile),200);
}
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMessage()),422);
}
});
//minimal code to get started ...
I am posting the data from Android to the Node.js. I am successfully able to call the Node.js post method and using restify able to get the Post data.
But when doing through express I am not able to get the post body in Node.js. I tried for many approaches from SO post but it seems none are working may be I am missing something.
The snippets are like:
Node.js
var express = require('express')
var request = require('request')
var http = require('http')
var bodyParser = require('body-parser')
var app = express();
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname + '/public'))
app.use(bodyParser.urlencoded())
app.use(bodyParser.json())
app.post('/search/addcomplaint',addComplaint)
function addComplaint(req,res,next){
console.log(req.body);
if (!req.body) return res.sendStatus(400)
res.send(201,user)
}
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})
at Android Site I am making a retrofit call like this:
#Multipart
#POST("/search/addcomplaint")
public User search(#Part("complaint") String complaint);
when I used restify in Node.js I was able to get req.body but using express I am not getting the request body.
I want to convert and image into base64 format and then upload that onto the server but the base64 string is so large that it gives me error on web that parameter not received.Any idea to send the complete string to the server using the below webservice? Here is below service that i tried.
var xhrAddclient = Titanium.Network.createHTTPClient();
xhrAddclient.open('POST', webservice_url);
xhrAddclient.send({
method : "addclient",
image : base64string,
});
xhrAddclient.setTimeout(10000);
xhrAddclient.onerror = function() {
showAlertBox('Service timed out. Please try again.');
};
xhrAddclient.onload = function() {
showAlertBox("Client added successfully.");
};
Check here: http://www.smokycogs.com/blog/titanium-tutorial-how-to-upload-a-file-to-a-server/
and here: https://wiki.appcelerator.org/display/guides/File+Uploads+and+Downloads#FileUploadsandDownloads-Fileupload
or just:
xhr.setRequestHeader("Content-type", "multipart/form-data");
xhr.send(file);