0

I am looking to use Deviantsart's public API in order to host some images for a project I am working on.

The issue I am having is I am not getting the JSON result back with the URL but instead getting the entire webpage.

their instructions are relatively simple

Upload using our public REST API: POST http://deviantsart.com yourimage.jpg

Then returns a JSON-Result with the URL:

{ "url" : "urltoimage" }

Here is my code.

WebAPI

[HttpPost]
    public async Task<HttpResponseMessage> UploadGliderData()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        }
        var httpPostedFile = HttpContext.Current.Request.Files["file"];
        using (var client = new HttpClient())
        {
            using (var content =
                new MultipartFormDataContent())
            {
                var image = new Bitmap(httpPostedFile.InputStream);
                var stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                var imgContent = new StreamContent(stream);
                imgContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
                content.Add(imgContent);
               
                using (
                   var message =
                       await client.PostAsync("http://deviantsart.com", content))
                {
                    var input = await message.Content.ReadAsStringAsync();
                    return this.Request.CreateResponse(HttpStatusCode.OK, input);

                }
            }
        }
    }

AngularJS Controller

function upload(files) {
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            Upload.upload({
                url: '/api/SGWebAPI/UploadGliderData',
                method: 'POST',
                data: $scope.glider,
                file: file
            })
            .success(function (data, status, headers, config) {
                debugger;
                console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
            }).error(function (data, status, headers, config) {
                debugger;
                console.log('error status: ' + status);
            });
        }
    }

Result

The value for input can be found Here

Thank you in advance

4
  • Maybe it isn't posting as multi-part form data so you are hitting that first if block, but you aren't returning from it so it continues to process. Seems like you need a return statement for the UnsupportedMediaType. Might not be your problem but if you get that response type afterwards at least you narrowed down what might be happening. Commented Jul 17, 2015 at 18:43
  • Why even post to your api to post to theirs? Just change your javascript URL to theirs. Commented Jul 17, 2015 at 19:23
  • @JeffTreuting Thank you for your comment, just checked and I am not hitting the first IF statement therefore it is posting as multi-part form data Commented Jul 17, 2015 at 20:53
  • @acastr7 I tried doing that and I kept getting this error. XMLHttpRequest cannot load deviantsart.com. Request header field Authorization is not allowed by Access-Control-Allow-Headers. I tried a number of things but to no anvil. Commented Jul 17, 2015 at 20:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.