Attach Resume File using c#- .Net

Forum for users and developers of Bullhorn's API service.

Moderators: StaffingSupport, s.emmons, BullhornSupport

Post Reply
mvcinoc
User
Posts: 10
Joined: Sun Oct 20, 2013 2:59 pm

Attach Resume File using c#- .Net

Post by mvcinoc » Mon Jun 23, 2014 10:34 am

I'm writing a method using c# to attach a resume - using the REST API.

I wrote a method (below) to upload a .doc file as a multipart/form attachment. I run the code below an get a 500.

From a .Net perspective Is HttpClient the best way to accomplish this?

Does anyone have .Net code you could share to accomplish the attaching a resume file?

What am I doing wrong here?

Any help will be greatly appreciated.

Erik

========================
Here is my code:
========================

public bool AttachResume(int candidateID, string uploadPath)
{
bool attached = false;

//Build URL...
var restToken = this.GetRestTokenPublic();
string bhRestTokenEnd = string.Format(@"&BhRestToken={0}", restToken.BhRestToken);
string urlMid = string.Format("file/Candidate/{0}/raw?externalID=Portfolio&fileType=SAMPLE", candidateID);
string putURL = @String.Format("{0}{1}{2}", restToken.RestUrl, urlMid, bhRestTokenEnd);
//the put URL looks like:
//https://rest0.bullhornstaffing.com/rest ... xxxxxxxxxx

var client = new HttpClient();
var content = new MultipartFormDataContent();
content.Add(new StreamContent(File.Open(uploadPath, FileMode.Open)), "resume", "resume.docx");

var result = client.PutAsync(putURL, content);
var putResult = result.Result;
var stringResult = result.Result.ToString();

if (putResult.IsSuccessStatusCode)
attached = true;

return attached;
}

raleigh
User
Posts: 13
Joined: Mon Apr 28, 2014 4:24 pm

Re: Attach Resume File using c#- .Net

Post by raleigh » Mon Jun 30, 2014 3:24 pm

Hello Erik!

My name is Raleigh and I will be answering your REST API question.

HttpClient is for .Net. However, I have no personal experience using it for REST. We use a Chrome extension call PostMan that is free and builds directly into your browser.

I brought up the documentation to take a look at your /put URL and found a couple things that may be worth noting/trying.


Pages 44-46 of the REST documentation give some sample URL's in using the /put function:
http://developer.bullhorn.com/sites/def ... TAPI_0.pdf

Here is your /put URL:
//https://rest0.bullhornstaffing.com/rest ... xxxxxxxxxx

For a DOC file, you would specify a contentType of application/msword.

In your URL, you have fileType after externalID. Try swapping the two an un-capitalizing "portfolio."

For example:
//https://rest0.bullhornstaffing.com/rest ... xxxxxxxxxx

Pages 45 and 46 have more examples for you to look over.


I hope this helps!

srg.bdavis
User
Posts: 67
Joined: Thu Feb 20, 2014 4:52 pm

Re: Attach Resume File using c#- .Net

Post by srg.bdavis » Mon Jun 30, 2014 6:01 pm

Erik,

I believe I ran into the same problem trying to use the multipart/form attachment so I ended up uploading it as a base-64 string inside a JSON object.

Here is the method we currently use on our site:

Code: Select all

public async Task<bool> UploadResume(int candidateId, byte[] resume, string fileName, string fileExtension, string description)
        {
            if (_oAuthProvider.RestUrl == null)
            {
                await _oAuthProvider.Login();
            }

            var upload = new FileUpload();
            upload.FileContent = Convert.ToBase64String(resume);
            upload.ExternalID = "portfolio";
            upload.Name = fileName;
            upload.FileType = "SAMPLE";
            upload.Description = description;
            upload.Type = "Resume";

            switch (fileExtension)
            {
                case("doc"):
                    upload.ContentType = "application/msword";
                    break;
                case("docx"):
                    upload.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case("pdf"):
                    upload.ContentType = "application/pdf";
                    break;
                case("txt"):
                    upload.ContentType = "text/plain";
                    break;
                case("rtf"):
                    upload.ContentType = "application/rtf";
                    break;
                case("odt"):
                    upload.ContentType = "application/odt";
                    break;
                case("html"):
                    upload.ContentType = "text/html";
                    break;
                case("htm"):
                    upload.ContentType = "text/html";
                    break;
                default:
                    upload.ContentType = "text/plain";
                    break;
            }

            var requestUri = new StringBuilder(_oAuthProvider.RestUrl);
            requestUri.Append(string.Format("file/Candidate/{0}?",candidateId));
            requestUri.Append(string.Format("&BhRestToken={0}", _oAuthProvider.BhRestToken));

            using (var client = new HttpClient())
            {
                var response = await client.PutAsJsonAsync(requestUri.ToString(), upload);

                if (response.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                {
                    var error = await response.Content.ReadAsAsync<ApiError>();
                    throw new Exception(string.Format("Unable to attach resume to candidate: {0}", error.ConvertDetailToString()));
                }
            }
        }
The "FileUpload" object I am creating is not the actual FileUpload class under System.Web.UI.WebControls ( I really should have picked a better name). It is just an arbitrary object used to store everything I want serialized to JSON in the httpclient putAsJsonAsync method.

Brent

mgeltser
User
Posts: 19
Joined: Wed Dec 31, 1969 8:00 pm

Re: Attach Resume File using c#- .Net

Post by mgeltser » Mon Jul 28, 2014 7:21 pm

Brent,

Thanks for an example.
What version of .Net framework are you using?
I am having problems referencing HttpClient. It should be a part of System.Web.Http, but this library is not part of my deployment. Did you have to install anything to get this library?

Your answer will be much appreciated.
Thanks,
Mike

srg.bdavis
User
Posts: 67
Joined: Thu Feb 20, 2014 4:52 pm

Re: Attach Resume File using c#- .Net

Post by srg.bdavis » Mon Jul 28, 2014 10:30 pm

Mike,

I am using .Net 4.5. I believe HttpClient should be in System.Net.Http.

Note: There is a small bug in that code I posted. In the if (response.IsSucccessStatusCode) else statement, reading the ApiError object for the exception only works if you get some type of error message from the Bullhorn API in an http 400 response. If you get anything other than a 400, the code blows up with an UnsupportedMediaTypeException since the response does not fall into the expected format. There really should be an additional check there to make sure the unsuccessful response is an error from the Bullhorn API. For anything else, don't try to format the response into that ApiError object and read it as a normal string.

Brent

mgeltser
User
Posts: 19
Joined: Wed Dec 31, 1969 8:00 pm

Re: Attach Resume File using c#- .Net

Post by mgeltser » Mon Jul 28, 2014 11:28 pm

Brent, thanks for your quick reply.

Strange, but System.Net.Http.HttpClient does not have PutAsJsonAsync function
http://msdn.microsoft.com/en-us/library ... .110).aspx

I referenced the dll, but no such method...

Are you sure you are not referencing something custom? Maybe NuGet or some other package?

Mike

srg.bdavis
User
Posts: 67
Joined: Thu Feb 20, 2014 4:52 pm

Re: Attach Resume File using c#- .Net

Post by srg.bdavis » Tue Jul 29, 2014 4:13 pm

Mike,

It looks like PutAsJsonAsync is in the HttpClientExtensions class from the System.Net.Http.Formatting assembly.

Brent

mgeltser
User
Posts: 19
Joined: Wed Dec 31, 1969 8:00 pm

Re: Attach Resume File using c#- .Net

Post by mgeltser » Wed Jul 30, 2014 3:22 pm

Thanks Brent,

I did some research and it looks like this is part of a NuGet package for version 4.5
Unfortunately my hosting is at version 4.0, so I can not install that package.
And I haven't figured out how to get this to work without the package yet.
I was able to do everything but upload a resume :-(

If by ay chance you have a working project that you can email me I would very much appreciate that.
My email is: mgeltser@jacobsononline.com

Thanks,
Mike

Post Reply