Home > Mobile >  422 unprocessable entity error received from pastebin POST request
422 unprocessable entity error received from pastebin POST request

Time:02-17

I've been making a big project recently and have been trying to figure out how to make my C# .Net program post a string to Pastebin and return the URL of the paste created. I've gotten a few lines of code down (not mine, I found it on this site) but even after much troubleshooting I always get some kind of error from it. I'm not familiar with webrequest or any of the similar methods designed to interact with the internet, so I had to rely on advice from other people online, yet even after multiple hours worth of searching online for solutions to my problem I still get a (422) unprocessable entity error from my code when run:

using System.Net;
using System.IO;
using System.Text;
using System.IO;

public class Program
{
    public static void Main()
    {
        WebRequest wr = WebRequest.Create("https://pastebin.com/api/api_post.php");
        //ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] bData = Encoding.UTF8.GetBytes(string.Concat("api_option=paste&api_paste_code=", Console.ReadLine(), "&paste_private=0&api_dev_key="   api_dev_key));
        wr.Method = "POST";
        wr.ContentType = "application/x-www-form-urlencoded";
        wr.ContentLength = bData.Length;
        Stream sMyStream = wr.GetRequestStream();
        sMyStream.Write(bData, 0, bData.Length);
        wr.GetResponse();
        sMyStream.Close();
    }
}

Exact error:

Run-time exception (line 36): The remote server returned an error: (422) Unprocessable Entity.

Stack Trace:

[System.Net.WebException: The remote server returned an error: (422) Unprocessable Entity.]
   at System.Net.HttpWebRequest.GetResponse()
   at Program.Main() :line 36

I've decided the best course of action to take to solve my problem is just to get help directly from people who know this kind of stuff well. I've just been running this code on dotnetfiddle. If you need more information please ask.

Edits: updated a few parameters (such as api_option)

CodePudding user response:

After taking a look at the docs I think you're using a unsuitable parameter (paste_subdomain) for posting your API key.

Have a try using api_dev_key instead:

byte[] bData = Encoding.UTF8.GetBytes(string.Concat("paste_code=", encoded, "&paste_private=0&paste_expire_date=1D&api_dev_key="   api_dev_key));

If this doesn't work, better have a look at the other parameters, too.


Just a blind guess, but probably the error occurs because you don't url-encode the code to be posted.

Maybe it'll do the trick if you encode the code entered:

var toBePosted = Console.ReadLine();
var encoded = HttpUtility.UrlEncode(toBePosted);
byte[] bData = Encoding.UTF8.GetBytes(string.Concat("paste_code=", encoded, "&paste_private=0&paste_expire_date=1D&paste_subdomain="   api_dev_key));

CodePudding user response:

I've had working code for a while, and just recently got this error showing up on one of my bots that paste to pastebin.. worked fine @ 1230MST started erroring @ 100pm MST..

Could be something on their end, and nothing has changed here, and its worked for many years.

  • Related