When the service is being called by using method as GET, it works smoothly i.e. request.getParameter("userValue") is printed.
But while using Post method, it prints null for request.getParameter("userValue").
HTML code : (jsonObject has valid json)
var myData = "userValue=" + jsonObject ;
jQuery.ajax({
type: "POST",
url: "http://localhost:8080/Webservice_JS_26Oct/FieldsToFile/write",
data: myData,
contentType: "application/json; charset=utf-8",
dataType: "json",
Java Code:
#RequestMapping(value = "/FieldsToFile")
public class FileWriter {
#RequestMapping(value = "/write", method = RequestMethod.POST, produces = "application/json")
public String getData(HttpServletRequest request) throws IOException, IllegalStateException, ServletException {
String jsonString = request.getParameter("userValue") ;
System.out.println("jsonString = " + jsonString);
String myData = request.getParameter("myData") ;
I am new to this, Please advise how to make it work for POST method.
You can use request.getInputStream() to print the request body part. I suggest that your contentType: "application/json; charset=utf-8", can be application/x-www-form-urlencoded or HttpServletRequest request can be #Requestbody ..
Related
Can not pass a list of strings to a Web API endpoint. Why?
Here is my controller:
[Route("[controller]")]
[ApiController]
public class MyController
{
[HttpPost("foo")]
public string MyMethod(List<string> strs)
{
return "foo";
}
}
Here is how I am trying to call it:
var strs = new List<string> { "bar" };
var json = JsonConvert.SerializeObject(strs);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpCliet.PostAsync("/My/foo", content);
Before calling the endpoint I place a breakpoint on the return "foo"; line. Once the breakpoint is hit the strs list inside the MyController.MyMethod is empty. The strs is not null, but it contains no elements. While my intentions and expectations are to see the strs containing one element, i.e. the string "bar".
I am using the ASP.NET Core 2.2 in project where I create and use the HttpClient. And I am using the same ASP.NET Core 2.2 in project where I have the endpoint.
I am not sure what is wrong here. I have checked a few sources. E.g. the following:
C# HTTP post , how to post with List<XX> parameter?
https://carldesouza.com/httpclient-getasync-postasync-sendasync-c/
https://blog.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/
And I can not find what I am missing according to those resources.
UPDATE
The following call works for me as expected:
var json = JsonConvert.SerializeObject(string.Empty);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await server.CreateClient().PostAsync("/My/foo?strs=bar", content);
Maybe someone knows why the parameters in my case are read from the query string only, but not from body?
You can change your url to a full url in client.PostAsync.Here is a demo worked:
Api(localhost:44379):
WeatherForecastController:
[HttpPost("foo")]
public string MyMethod(List<string> strs)
{
return "foo";
}
Call(localhost:44326):
public async Task<IActionResult> CheckAsync() {
HttpClient client = new HttpClient();
var strs = new List<string> { "bar","bar1","bar2" };
var json = JsonConvert.SerializeObject(strs);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://localhost:44379/WeatherForecast/foo", content);
return Ok(response);
}
result:
I am making a SOAP Request and this is how my request should be sent:
<id></id>
<fieldList>
<string>string</string>
<string>string</string>
</fieldList>
This is how I have built my envelope:
final int id = 21;
List<String> fieldList = new List<String>();
fieldList = [
"pinNumber:PIN0000074",
"dispatchArrivedTime:13.05",
"towedStatus:C"
];
var envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<update xmlns=\"http://example.com/\">"
"<id>${id}</id>"
"<fieldList>${fieldList}</fieldList>"
"</update>"
"</soap:Body>"
"</soap:Envelope>";
final response = await http.post(
'http://example.com/vc/ws/towedvehicletable.asmx',
headers: {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": "http://example.com/update",
"Host": "example.com"
//"Accept": "text/xml"
},
body: envelope);
However, this approach does not work. It would be really helpful if somebody could show me how to pass a String List into my request. I am using Flutter and Dart. Thanks
Just map list of strings and then join it:
List<String> fieldList = ['test1', 'test2'];
final xmlValues = fieldList.map((v) => '<string>$v</string>').join();
print(xmlValues);
prints:
<string>test1</string><string>test2</string>
There's also a package for working with XML. It allows you to both parse and construct XML documents.
I am using Spring 4 latest, and I generally have no problem writing RESTful controllers. There is a legacy web-app, which is using java.net.HTTPUrlConnection to do a multi-part upload. There are 3 pieces of data we are uploading:
1 is a PDF file, and we have the bytes, then the other two pieces of data are just 2 string fields.
First let me show you the Spring REST controller to accept the data:
#RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
public #ResponseBody boolean saveData(#RequestPart(value = "field1") String field1, #RequestPart(value = "field2") String field2, #RequestParam(value = "pdfbytes") String pdfbytes)
{
System.out.println("saveData: field1=" + field1);
System.out.println("saveData: field2=" + field2);
System.out.println("saveData: pdfbytes=" + pdfbytes);
boolean response = true;
return response;
}
The code in front-end, for sending the data using 'java.net.HttpURLConnection'
looks like this:
String boundary = MultiPartFormOutputStream.createBoundary();
URL uploadDocumentUrl = new URL(protocol + "://" + host + UPLOAD_EDITED_DOCUMENT);
HttpURLConnection urlConn = (HttpURLConnection) MultiPartFormOutputStream.createConnection(uploadDocumentUrl);
urlConn.setRequestProperty("Content-Type", MultiPartFormOutputStream.getContentType(boundary));
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
urlConn.setRequestProperty("User-Agent", userAgent);
urlConn.setRequestMethod("POST");
MultiPartFormOutputStream out = new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
String pdfbytes= getEncodedDocument(pdf);
out.writeField("field1", field1);
out.writeField("field2", field2);
out.writeField("pdfbytes", pdfbytes);
out.close();
int responseCode = urlConn.getResponseCode();
String responseMessage = urlConn.getResponseMessage();
"MultiPartFormOutputStream" is a custom object that was created to send data via HttpUrlConnection, it's pretty standard code. I do trust it at this time.
So, based on how we are sending the data, do I need to change the Controller to do anything different, or does that look ok?
Now here is the code, that I am using to Unit Test that controller:
#Test
public void testMockUpload() throws Exception
{
// Load resource being uploaded
byte[] pdfbytes = getByteArrayFromFile(FILENAME);
MockMultipartFile firstFile = new MockMultipartFile("field1", "", "text/plain", "field1 data".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("field2", "", "text/plain", "field2 data".getBytes());
MockMultipartFile jsonFile = new MockMultipartFile("pdfbytes", "", "text/plain", pdfbytes);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.fileUpload(BASE_URL + "/save").file(firstFile).file(secondFile).file(jsonFile)
.with(user(USERNAME).roles("role1", "role2")).contentType(MediaType.MULTIPART_FORM_DATA_VALUE);
this.mockMvc.perform(requestBuilder).andDo(print()).andExpect(status().isOk());
}
And the error I get back now, is:
org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException
which I am looking into. If I need to make any changes on how I need to create my test, I am very open to that. Eventually, I will get everything to sync up between the sending code, the receiving controller, and the unit test.
Thanks in advance! As usual, if there is any other data, or information, I can provide, please let me know. Thanks!
To upload one file you would define the RequestParam type as org.springframework.web.multipart.MultipartFile;
#RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
public #ResponseBody boolean saveData(#RequestParam(value = "file") MultipartFile file)
{
return response;
}
For Multiple files I'd try creating a Wrapper form:
public class UploadForm{
private List<MultipartFile> files;
}
Bind to this in the controller:
#RequestMapping(value = "/save", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
public #ResponseBody boolean saveData(#ModelAttribute uploadForm)
{
return response;
}
and then use Spring's support for indexed fields to bind to a collection:
Test:
MockMultipartFile firstFile = new MockMultipartFile("files[0]", "", "text/plain", "field1 data".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("files[1]", "", "text/plain", "field2 data".getBytes());
MockMultipartFile jsonFile = new MockMultipartFile("files[2]", "", "text/plain", pdfbytes);
Client:
out.writeField("files[0]", file1Bytes);
out.writeField("files[1]", file2Bytes);
...
I am developing a Restful service in C# and working well when I use
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle =
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
string jdata(string id);
and my corrsponding function implementation is:
public string json(string id)
{
return "You Typed : "+id;
}
Up to here all works well,
but when I change WenInvoke Method="POST" I have to face a "Method NOT Allowed.";
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle =
WebMessageBodyStyle.Wrapped, UriTemplate = "json/?id={id}")]
string jdata(string id);
You get "Method not allowed" because you are reaching the Uri "json/?id={id}" via GET instead of POST.
Check this with your client (you didn't mention how you call this resource). Please give some further details how you are trying to use your web service in client. Is is .Net client?
To test your API I recommend using Fiddler - when you can explicitly specify whether to use GET or POST before sending an http request:
Another thing is, you might have unwittingly used "json" as Uri, but defined ResponseFormat as WebMessageFormat.Xml. Isn't it a little confusing for the client? Maybe you wanted to return JSON back? In that case, I would recommend using Json in both - request and response:
[WebInvoke(Method = "POST", UriTemplate = "/ValidateUser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
[WebInvoke(Method="POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "json")]
string jdata(string id);
This is how your contract should look like and then in client
WebRequest httpWebRequest =
WebRequest.Create(
url);
httpWebRequest.Method = "POST";
string json = "{\"id\":\"1234"\}"
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
httpWebRequest.Timeout = 1000000;
WebResponse webrespon = (WebResponse)httpWebRequest.GetResponse();
StreamReader stream = new StreamReader(webrespon.GetResponseStream());
string result = stream.ReadToEnd();
Console.Out.WriteLine(result);
Above is just something i use to test my services. Hope it helps.
I am using jQuery ajax to calling a web service method but is is not doing and generating error..
The code is here for jQuery ajax in asp page
var indexNo = 13; //pass the value
$(document).ready(function() {
$("#a1").click(function() {
$.ajax({
type: "POST",
url: "myWebService.asmx/GetNewDownline",
data: "{'indexNo':user_id}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#divResult").text(msg.d);
}
});
});
});
and this is the is web service method
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data;
using System.Web.Script.Serialization;
using TC.MLM.DAL;
using TC.MLM.BLL.AS;
/// <summary>
/// Summary description for myWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class myWebService : System.Web.Services.WebService
{
public myWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetNewDownline(string indexNo)
{
IndexDetails indexDtls = new IndexDetails();
indexDtls.IndexNo = "13";
DataSet ds = new DataSet();
ds = TC.MLM.BLL.AS.Index.getIndexDownLineByIndex(indexDtls);
indexNoDownline[] newDownline = new indexNoDownline[ds.Tables[0].Rows.Count];
for (int count = 0; count <= ds.Tables[0].Rows.Count - 1; count++)
{
newDownline[count] = new indexNoDownline();
newDownline[count].adjustedid = ds.Tables[0].Rows[count]["AdjustedID"].ToString();
newDownline[count].name = ds.Tables[0].Rows[count]["name"].ToString();
newDownline[count].structPostion = ds.Tables[0].Rows[count]["Struct_Position"].ToString();
newDownline[count].indexNo = ds.Tables[0].Rows[count]["IndexNo"].ToString();
newDownline[count].promoterId = ds.Tables[0].Rows[count]["PromotorID"].ToString();
newDownline[count].formNo = ds.Tables[0].Rows[count]["FormNo"].ToString();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
JavaScriptSerializer js = new JavaScriptSerializer();
string resultedDownLine = js.Serialize(newDownline);
return resultedDownLine;
}
public class indexNoDownline
{
public string adjustedid;
public string name;
public string indexNo;
public string structPostion;
public string promoterId;
public string formNo;
}
}
Please help me something.
There is a problem with your input JSON data. Instead of manual serialization you should try to use the builtin JSON class. Ex.
$.ajax({
...
data: JSON.stringify({ indexNo: user_id }),
...
});
This should fix your problem.
You should change the art how you implement serialization in the server. Just add ScriptMethod attribute with ResponseFormat = ResponseFormat.Json. Using of HTTP GET is also possible with respect of additional (optional) attribute UseHttpGet = true
[WebMethod]
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetNewDownline(string indexNo)
The database access can be also improved if you will use SqlConnection, SqlCommand and SqlDataReader classes or another versions of DbDataReader depends of you database source (OleDbDataReader, OracleDataReader, OdbcDataReader, DataTableReader). If you prefer work with strong typed data, then SqlDataAdapter generated by Visual Studio will be better version of DataSet. To do this just add a new Item to your profject, choose "Data" / "DataSet" and then add a Query or TableAdapter.
If you decide to use HTTP GET you should don't make corresponding modification of your web.config file.
<configuration>
<!-- ... -->
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
<!-- ... -->
</system.web>
</configuration>
About usage of JSON.stringify I have the same opinion like "egyedg".
I recommend you to look following links:
Can I return JSON from an .asmx Web Service if the ContentType is not JSON?
How do I build a JSON object to send to an AJAX WebService?
JQuery ajax call to httpget webmethod (c#) not working