The development environment is ASP.NET(C#) and I develop REST API.
And use curl command for calling the api in integration test.
What I want to know is the following things.
At one request,
it include two binary data files in an HTTP request body
and want to post it.
By the way, I know the method to post one binary data file.
The curl commnad is [--data -binary "#filePath"]
The mehod of ApiControlleris [public HttpResponseMessage Post(dyte[] data1)
The binary data of the file are included in variable data1.
It would be greatly appreciated if you could explain the details.
Related
I want to run a curl request given as
curl --data-binary #"/path/to/my.pdf" -H "Content-Type: application/pdf" -L "http://pdfx.cs.man.ac.uk"
This request simply sends a PDF file to http://pdfx.cs.man.ac.uk and in response this site return a XML file.
How it can be done in C#?
If I understand correctly, you want to reproduce form POST action in C#. One way is to use HttpClient from this package (.NET 4.0) or directly if working in .NET 4.5+.
A fully working example can be found here. Basically you have to:
you initialize the http client
initialize form data
post the data
wait for the response
You can also set the content type of posted content (in your case application/pdf), by following the provided answer from this question.
I'm rather new to sending/receiving over networks/sockets/network streams and so on.
I'm making an IRC program that can communicate with Twitch.tv. They have an API, and they have examples of all sorts of requests you would use to get different kinds of information.
https://github.com/justintv/Twitch-API/tree/master/v3_resources
One example of their requests is this:
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user
I have tried to do some research on requests, and I sort of understand some, but for the most part I could not find any resources that help make it click for me.
In the above example, what are the important parts of that request? curl? -H? Is that one big command, or is it two commands separated by the \ at the end of the first line?
Then, the biggest question, how to send requests like the one above using C#?
EDIT 1:
I also know that I will be getting responses in JSON. Is there anything built in that assists with receiving/parsing JSON?
And also using PUT to change some JSON? (some things in the API allow PUT).
For the first bit of the question, you asked what are the important parts
It has an accept header of application/vnd.twitchtv.v3+json
It is a GET request
The api url: https://api.twitch.tv/kraken/chat/kraken_test_user
This request in c# could look like the following (could because there is more than one way to do it)
private async Task<object> GetRequest(string url)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v3+json"));
var response = await httpClient.GetAsync(url);
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
Note that the files in the link you posted are to Mark Down files that Google describes as:
MD, or markdown document is a text file created using one of several possible dialects of the Markdown language. MD files use plain text formatting but includes inline text symbols that define how to format the text, and is designed for authoring plain text documentation that can be easily converted to HTML.
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user
http://curl.haxx.se/docs/manpage.html explains what the curl command is that then has 2 switches, H and X. where quoting the link:
-H, --header
(HTTP) Extra header to include in the request when sending HTTP to a
server. You may specify any number of extra headers. Note that if you
should add a custom header that has the same name as one of the
internal ones curl would use, your externally set header will be used
instead of the internal one. This allows you to make even trickier
stuff than curl would normally do. You should not replace internally
set headers without knowing perfectly well what you're doing. Remove
an internal header by giving a replacement without content on the
right side of the colon, as in: -H "Host:". If you send the custom
header with no-value then its header must be terminated with a
semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".
curl will make sure that each header you add/replace is sent with the
proper end-of-line marker, you should thus not add that as a part of
the header content: do not add newlines or carriage returns, they will
only mess things up for you.
See also the -A, --user-agent and -e, --referer options.
Starting in 7.37.0, you need --proxy-header to send custom headers
intended for a proxy.
Example:
# curl -H "X-First-Name: Joe" http://192.168.0.1/
WARNING: headers set with this option will be set in all requests -
even after redirects are followed, like when told with -L, --location.
This can lead to the header being sent to other hosts than the
original host, so sensitive headers should be used with caution
combined with following redirects.
This option can be used multiple times to add/replace/remove multiple
headers.
The "\" makes the next line be added to the first line.
-X, --request
(HTTP) Specifies a custom request method to use when communicating
with the HTTP server. The specified request method will be used
instead of the method otherwise used (which defaults to GET). Read the
HTTP 1.1 specification for details and explanations. Common additional
HTTP requests include PUT and DELETE, but related technologies like
WebDAV offers PROPFIND, COPY, MOVE and more.
Normally you don't need this option. All sorts of GET, HEAD, POST and
PUT requests are rather invoked by using dedicated command line
options.
This option only changes the actual word used in the HTTP request, it
does not alter the way curl behaves. So for example if you want to
make a proper HEAD request, using -X HEAD will not suffice. You need
to use the -I, --head option.
The method string you set with -X will be used for all requests, which
if you for example use -L, --location may cause unintended
side-effects when curl doesn't change request method according to the
HTTP 30x response codes - and similar.
(FTP) Specifies a custom FTP command to use instead of LIST when doing
file lists with FTP.
(POP3) Specifies a custom POP3 command to use instead of LIST or RETR.
(Added in 7.26.0)
(IMAP) Specifies a custom IMAP command to use instead of LIST. (Added
in 7.30.0)
(SMTP) Specifies a custom SMTP command to use instead of HELP or VRFY.
(Added in 7.34.0)
If this option is used several times, the last one will be used.
In C#, there is a WebRequest class that https://msdn.microsoft.com/en-CA/library/456dfw4f(v=vs.110).aspx has a good example of how to use to get data from a given URL.
As for handling JSON, please look into http://www.newtonsoft.com/json which is a rather common library used for parsing JSON responses. PUT would be the HTTP verb like GET or POST used to tell the server how to process a request. I'd suggest in the future be careful about posting a rather broad set of questions here as I could see this being something that a class could spend an hour covering somewhere that I doubt your intention is getting someone else to do your homework, right?
So I've been working on an api that, using RESTful, allows the user to get data from a database. Pretty simple, URL is along the lines of local:port/projects/[id of project]. The api returns some xml with 4 or 5 results.
What I'm having trouble with is PUT. As far as I understand it, I should use the same url, but use the PUT request method, and include the data that I want sent in a parameter. The problem seems to be that when I run the PUT, it just returns the same data as a GET.
I'm using the following site to test this: wst dot mytechlabs dot com (won't let me post two links here;?)
The code for my controller is located here: http://pastebin.com/3HXXR4YY
Thanks in advance, I'll monitor this, so let me know if I forgot any info that would help.
I am building a tool to translate some CSV files into XML files and then upload them via the Amazon Web Services to upload products to a market place store.
I am testing the tool and the CSV to XML translation is fine and valid xml is created but I am having trouble calling the AWS services and sending the files. At the moment I can't even succesfully call the AWS service let alone try sending a file.
When I create a HttpWebRequest and try and call the method "GetRequestStream" I get a WebException:
"The remote name could not be resolved https"
There is no inner exception or anything useful to help me.
The properties I have set to the webRequest are
Address - https://mws.amazonservices.co.uk/Action=SubmitFeed&MarketplaceIdList.Id.1=MarketPlaceId&Merchant .................
Headers -
+ Headers {User-Agent: Funkyrox/1.0 (Language=C#; =)
Content-Type: application/octet-stream
Content-MD5: 4BDPHUko1//iVAdb5wr/Vw==
Host: mws.amazonservices.co.uk
} System.Net.WebHeaderCollection
Host -
"mws.amazonservices.co.uk"
Method - POST
RequestUri -
https://mws.amazonservices.co.uk/Action=SubmitFeed&MarketplaceIdList.Id.1=MarketPlaceId&Merchant......................
There are others if needed as well.
When I ping https://mws.amazonservices.co.uk I get a response so I think the it's valid
I can't seem to find really useful examples of how to do this. I downloaded an C# tool from Amazon and have used a lot of the code in there but can't see how to call the service correctly.
Anyone with any ideas?
This is my first time developing this kind of system, so many of these concepts are very new to me. Any and all help would be appreciated. I'll try to sum up what I'm doing as efficiently as possible.
Background: I have a web application running AngularJS with Bootstrap. The app communicates with the server and DB through a web service programmed using C#. On the site, users can upload files and reference them later using direct links. There's no restriction to file type (yet), so just about anything is allowed.
My Goal: Having direct links creates a big security problem for me, since the documents/images are supposed to be private data. What I would prefer to do is validate a user's credentials when the link is clicked, then load the file in the browser using a more generic url path.
--Example--
"mysite.com/attachments/1" ---> (Image)
--instead of--
"mysite.com/data/files/importantImg.jpg"
Where I'm At: Not very far. My first thought was to add a page that sends the server request and receives a file byte stream along with mime type that I can reassemble and present to the user. However, I have no idea if this is possible using a web service that sends JSON requests, nor do I have a clue about how the reassembling process would work client-side.
Like I said, I'll take any and all advice. I'd love to learn more about this subject for future projects as well, but for now I just need to be pointed in the right direction.
Your first thought is correct, for it, you need to use the Response object, and more specifically the AddHeader and Write functions. Of course this will be a different page that will only handle file downloads, so it will be perfectly fine in your JSON web service.
I don't think you want to do this with a web service. Just use a regular IHttpHandler to perform the validation and return the data. So you would have the URL "attachments/1" get rewritten to "attachments/download.ashx?id=1". When you've verified access, write the data to the response stream. You can use the Content Disposition header to set the file name.